Jump to content

Loading an array from a text file


Recommended Posts

Hello all,

I am completely new to the autoit language, but I have dabbled in other programming languages.

I would really appreciate any help getting my script to work. Here is what the script I am trying to make does:

1) Opens txt file formatted like a new array for each line like this:

["string1", "string2", "string3"]

2) Reads off a specified line from the file and types out the strings found in that array using send()

This is where I get errors. Let me show you what I have so far.

Dim $array[1]
$array = FileReadLine($file,$line)
$i = 0
While UBound($array) >= $i
    $string = $array[$i]
    Send($string)
    $i = $i + 1
WEnd

I've simplified it and changed the variable names so that hopefully it is easy to understand what I'm trying to do.

Thanks for any help you can give me.

Link to comment
Share on other sites

First, welcome to AutoIt :)

In your code, you Dim $array as a one dimension array, but then on the next line, you reassign $array to a string from the FileReadLine.

I think you meant to have it as $array[0] = FileReadLine($file,$line).

Also, if you are going to add more things to the $array, you will have to ReDim the array to be able to add new elements, it doesn't increase the size of the array by itself.

Hope that helps...

Edited by TheCuz

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
Share on other sites

You've declared your array with only one value. This means anything above $array[0] is not a valid variable. You should use _FileReadToArray() to populate an array with the file's contents, then step through it line by line.

#include <File.au3>

$file = "test.log"
Dim $array
_FileReadToArray($file,$array)
For $i = 1 To $array[0]
    $string = StringSplit(StringRegExpReplace($array[$i],'[]["]',""),",")
    For $x = 1 To $string[0]
        MsgBox(0,"",$string[$x])
    Next
Next
Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt
Link to comment
Share on other sites

Thanks for the fast replies. I see my mistake in making $array a string so using FileReadLine will not work. I looked up the help file for _FileReadToArray and it seems to be the right function to use. I tried using your code, but it gave me some errors (variable undefined) and I have no idea where to start fixing it. If anyone could explain exactly how this all works or if there is a better way to do this I would be extremely grateful.

Link to comment
Share on other sites

Thanks for the fast replies. I see my mistake in making $array a string so using FileReadLine will not work. I looked up the help file for _FileReadToArray and it seems to be the right function to use. I tried using your code, but it gave me some errors (variable undefined) and I have no idea where to start fixing it. If anyone could explain exactly how this all works or if there is a better way to do this I would be extremely grateful.

Make sure you are using the latest version of AutoIt, and the #include line must be at the top for the function to work. If you still have problems, can you paste your error?
Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...