Sign in to follow this  
Followers 0
Attckdog

[Help] Arrays with variables and loops

8 posts in this topic

Got a quick question about how to fill arrays with a loops.

I'm trying to read a large txt file and load its contents line by line into different arrays.

The reason I want to do this is to keep an easy way to tell what line the data came from.

I tried a few different methods to no real success got frustrated, so here I am.

My best bet as to how to do it:

$AAA = FileOpen("C:\Documents and Settings\dhicks\My Documents\TEST.txt")
; Check if file opened for reading OK
If $AAA = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf



For $Lines = 1 To 6000
$BBB = FileReadLine($AAA)
$CCC = $BBB[$Lines]
Next

For $Lines = 1 To 6000
_ArrayDisplay($CCC)
Next

So what I was aiming or was:

For every line of the file - read that line to get a string then load that line into an array.

If there is an easier way to do something like that i'm all for it.


A true renaissance man

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here's an easier way to read the file into an array, the $BBB[0] element holds the number of lines read.

#include<File.au3>
#include <Array.au3>
Global $BBB
$AAA = FileOpen("C:Documents and SettingsdhicksMy DocumentsTEST.txt")
; Check if file opened for reading OK
If $AAA = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileClose($AAA)
_FileReadToArray("C:Documents and SettingsdhicksMy DocumentsTEST.txt", $BBB)
_ArrayDisplay($BBB)

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I don't know why you need different arrays for each line. I would use one large array for all the lines. That said, I am not sure how big the file is or if the lines have hard returns but you could read it in as one string then use stringsplit($string,@crlf). You would end up with

array[0]=line 1

array[1]=line 2

etc.

Every line would have its own numeric spot in the array. What would having a seperate array add? If you needed something else, you could always create an associative array / dictionary.


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Also you probably don't want to display the entire array 600 times. _ArrayDisplay displays the whole array every time it's called. Check it.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Here's an easier way to read the file into an array, the $BBB[0] element holds the number of lines read.

I'm worried I was having a case of doing it the hard way eh?

Also you probably don't want to display the entire array 600 times. _ArrayDisplay displays the whole array every time it's called. Check it.

Yeah I was only doing that for testing purposes.

The complete script will just be used for collecting the data and exporting it in a more readable fashion.


A true renaissance man

Share this post


Link to post
Share on other sites

I'm worried I was having a case of doing it the hard way eh?

There are so many UDFs and functions available, it's hard to know which ones might be useful and what's available.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

***EDIT***

NVM found out I was ruining SciTE with too many consoleWrites lolz

******

New Problem Getting an error during my script. Runs fine for a little while. Then dies giving this Error. Is my array to huge or something?

C:Documents and SettingsdhicksMy DocumentsListingsChecker.au3 (114) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Edited by Attckdog

A true renaissance man

Share this post


Link to post
Share on other sites

Attckdog,

This usually means that you are making a reference outside of array bounds...where is the code?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
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
Sign in to follow this  
Followers 0