Sign in to follow this  
Followers 0
wizzardking

Help with arrays

7 posts in this topic

What I am trying to do is open a text file, sort it alphabetically and write it back to another text file in alpha order.

The error I get when I beta compile is $lArray[$a] improper format of array?

I get no error and no result when I beta run.

Any help I can get would be greatly appreciated!

here's what I wrote so far.

<------------------------------------ begin code:

#include <Array.au3>

Dim $lArray = 4000

$file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open config file.")

Exit

EndIf

$drive = FileReadLine($file)

FileClose($file)

;end get config

;open text file and read into array

$file = FileOpen($drive & "\inetpub\data\blognames.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open blognames file.")

Exit

EndIf

$a = -1

While 1

$a = $a + 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$lArray[$a] = $line

Wend

FileClose($file) ;end get text file

_ArraySort($lArray) ;sort array

_ArrayDisplay($lArray, "Class List of Active Window" ) ;display array in textbox (to be removed once working)

;write output file

$file = FileOpen($drive & "\inetpub\data\work\alpha.txt",2)

For $b = 0 to $a +1

FileWrite($file,$lArray[$b])

FileClose($file)

FileCopy($drive & "\inetpub\data\work\alpha1.tmp",$drive & "\inetpub\data\alpha.dat")

-------------------> end code

I know my programing style is very very old school, but thats where I came from mostly programing in a language called wccode for the old dos based wildcat bbs software and Quickbasic. So please don't judge the style just the syntax :whistle:

`Mitch

Share this post


Link to post
Share on other sites



1

when you are defining an array it would be

Dim $lArray[4000]

because

Dim $lArray = 4000 does not create an array and gives the value of 4000 to the "variable" $IArray

2

using " File Read to Array " should help

#include <file.au3>
#include <array.au3>

; dim the array name
Dim $aRecords

; read the drive/data
$file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open config file.")
    Exit
EndIf
$drive = FileReadLine($file)
FileClose($file)

; Check if file opened for reading OK
$file = FileOpen($drive & "\inetpub\data\blognames.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open blognames file.")
    Exit
EndIf

; read the file to an array
If Not _FileReadToArray($drive & "\inetpub\data\blognames.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf

; sort the array
_ArraySort($aRecords)
_ArrayDisplay($aRecords, "Array List")

; write the file
$file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2)
For $x = 1 To $aRecords[0]
    MsgBox(0, 'Record:' & $x, $aRecords[$x])
    FileWriteLine($file, $aRecords[$x])
Next

; close the file
FileClose($file)

*********** NOT TESTED

3

Nice try

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Ok all seems to work nicely for sorting the array. However, writing the array to a file is not working.

I noticed the line

For $x = 1 To $aRecords[0]

however when looking at the _arraydisplay box

the first line is blank. I'm assuming from looking at your code and from the help file that the $array[0] should contain a number corresponding to the number of elelments in the array? but in this it reurns zero. So it never writes the file. Do I need to define $array[0] or is this done automaticaly?

`Mitch

http://www.bytvdemand.com

1

when you are defining an array it would be

Dim $lArray[4000]

because

Dim $lArray = 4000 does not create an array and gives the value of 4000 to the "variable" $IArray

2

using " File Read to Array " should help

#include <file.au3>
#include <array.au3>

; dim the array name
Dim $aRecords

; read the drive/data
$file = FileOpen("c:\aubbs\gabbs.dat", 0) ;config file for getting drive letter

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open config file.")
    Exit
EndIf
$drive = FileReadLine($file)
FileClose($file)

; Check if file opened for reading OK
$file = FileOpen($drive & "\inetpub\data\blognames.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open blognames file.")
    Exit
EndIf

; read the file to an array
If Not _FileReadToArray($drive & "\inetpub\data\blognames.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf

; sort the array
_ArraySort($aRecords)
_ArrayDisplay($aRecords, "Array List")

; write the file
$file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2)
For $x = 1 To $aRecords[0]
    MsgBox(0, 'Record:' & $x, $aRecords[$x])
    FileWriteLine($file, $aRecords[$x])
Next

; close the file
FileClose($file)

*********** NOT TESTED

3

Nice try

8)

Share this post


Link to post
Share on other sites

I noticed that [1] actually equals the number of elements for some reason.

Ok all seems to work nicely for sorting the array. However, writing the array to a file is not working.

I noticed the line

For $x = 1 To $aRecords[0]

however when looking at the _arraydisplay box

the first line is blank. I'm assuming from looking at your code and from the help file that the $array[0] should contain a number corresponding to the number of elelments in the array? but in this it reurns zero. So it never writes the file. Do I need to define $array[0] or is this done automaticaly?

`Mitch

http://www.bytvdemand.com

Share this post


Link to post
Share on other sites

Ok changing these lines worked perfectly.

; write the file

$file = FileOpen($drive & "\inetpub\data\work\alpha.txt", 2)

For $x = 2 To $aRecords[1]

MsgBox(0, 'Record:' & $x, $aRecords[$x])

FileWriteLine($file, $aRecords[$x])

Next

; close the file

FileClose($file)

Thankyou for the help. and I learned some new commands too that will help me in other areas as well!

You guys are the best!!

`Mitch

http://www.tvbydemand.com

I noticed that [1] actually equals the number of elements for some reason.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

While i was having my coffee..... you figured it out

thats great!

Glad I could help

8)

BTW

$aRecords[0] should represent the number of elements, not [1]

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Hi,

either of these lines would fix your script properly [in fact both, to be accurate];

_ArraySort($aRecords,0,1) ;($array, $asc, $base)oÝ÷ ÚÊ2¢çhm²í¶»-zW¦z{ZÛaz{¦mêëzf¢ËazƦxCzh²+b¢}(­«­¢+Ù½ÈÀÌØíàôÄQ¼U  ½Õ¹ ÀÌØíI½É̤´Ä
use ubound(array)-1, and it does not need the artificially made [0] counter.

Best, Randall

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