Page 1 of 1 1
Topic Options
#34591 - 2002-12-21 10:12 PM Reading an array
Jair Offline
Fresh Scripter

Registered: 2002-08-13
Posts: 8
Hello. I'm new to kix, and am having trouble reading from an array. I think I've got it set up correctly, but when I try to display the contents element by element, UBound returns -1, so I cannot display anything. What am I doing wrong?

code:
Break on

$sUserText = "users.txt"

$iUserNum = 0
If Open(2, $sUserText, 2) = 0
Dim $aUserArray[0]
While $Error = 0
$sLine = ReadLine(2)
$error = @error
if $sLine <> ""
ReDim preserve $aUserArray[UBound($aUserArray)+1]
$aUserArray[UBound($aUserArray)] = $sLine
endif
Loop
Close(2)
Endif

MessageBox(UBound($aUserArray), "debug", 0)

for $iCurrentNum = 0 TO UBound($aUserArray)
MessageBox($aUserArray[$iCurrentNum], "debug", 0)
next

Users.txt is simply a text file with a user name on each line. Any help would be greatly appreciated. Thanks!
_________________________
Lost: [i]/dev/sanity
Top
#34592 - 2002-12-21 10:29 PM Re: Reading an array
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
If UBOUND returns -1 then it's not an array.

The code looks okay, so beef up the code with more erro coecing like this:
code:
Break on

Dim $aUserArray[0]

$sUserText = "users.txt"

$iUserNum = 0
If Open(2, $sUserText, 2) = 0
Dim $aUserArray[0]
While $Error = 0
$sLine = ReadLine(2)
? 'line = '+$sLine
$error = @error
if $sLine <> ""
? 'Ubound = '+ubound($aUserArray)
ReDim preserve $aUserArray[UBound($aUserArray)+1]
$aUserArray[UBound($aUserArray)] = $sLine
endif
Loop
Close(2)
Endif

MessageBox(UBound($aUserArray), "debug", 0)

for $iCurrentNum = 0 TO UBound($aUserArray)
MessageBox($aUserArray[$iCurrentNum], "debug", 0)
next

_________________________
There are two types of vessels, submarines and targets.

Top
#34593 - 2002-12-21 10:38 PM Re: Reading an array
Jair Offline
Fresh Scripter

Registered: 2002-08-13
Posts: 8
Thanks for the help! Everything looks ok to me when I make those changes. Now the first box reports ubound as being 0, and the second fires with nothing in it.
_________________________
Lost: [i]/dev/sanity
Top
#34594 - 2002-12-21 10:52 PM Re: Reading an array
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Then use DEBUG ON in the first line of code to step through the code. Also, are the read lines displayed?
_________________________
There are two types of vessels, submarines and targets.

Top
#34595 - 2002-12-21 10:56 PM Re: Reading an array
Jair Offline
Fresh Scripter

Registered: 2002-08-13
Posts: 8
It reads out the line and then the element number just fine. I've run it in debug mode more times than I can count, but can't find anything wrong. [Confused]
_________________________
Lost: [i]/dev/sanity
Top
#34596 - 2002-12-22 05:31 AM Re: Reading an array
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
It's a scope problem. Take your original dim of the array:
code:
Dim $aUserArray[0]

and put it before the if statement. It worked when I tested it.
_________________________
Eric

Top
#34597 - 2002-12-22 02:49 PM Re: Reading an array
Jair Offline
Fresh Scripter

Registered: 2002-08-13
Posts: 8
Yea! It works! [Big Grin] Many thanks all for the help!
_________________________
Lost: [i]/dev/sanity
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.034 seconds in which 0.016 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org