Jump to content

arrays nestled in for/next & do/until???


Recommended Posts

Hi,

in a text file, i have the following items

apple

orange

pear

orange

apple

cherry

my routine is:

to read each item into an array and within a "for / next" compare each line read to what was already stored and not store a duplicate. Each time I try to compare an array item to a variable within a for / next, it will always return true even though it sees the variable as 0 and or the read line variable and stored variable differ.

perhaps a bug?

i.e.

$thefile = FileOpen("d:\test.txt", 0)

$index=1

$match=0

while 1

$line = filereadline($thefile)

if @error = -1 then exitloop

$i=1

$match=0

for $i=1 to $index

if $line=$test[$i] then

$match=1

exitloop

endif

next

if $match=0 then

$test[$index]=$line

$index=$index + 1

endif

wend

for $i=1 to $index

msgbox(0,"Listing of Test",$test[$i],)

next

fileclose($thefile)

Link to comment
Share on other sites

Two potential problems:

1) Arrays are indexed starting at zero instead of one

Try for $i=0 to $index-1

2) When you do an equality comparison with a string and an empty array element, the result is always true because a numerical comparsion is used.... You can cast the array element to a string if String($line)=$test[$i] then OR you could make sure your for-loop bounds never compare $line to an empty element.

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

2)  When you do an equality comparison with a string and an empty array element, the result is always true because a numerical comparsion is used.... You can cast the array element to a string if String($line)=$test[$i] then  OR you could make sure your for-loop bounds never compare $line to an empty element.

.. OR .. I seem to recall a clever trick (which I haven't had a chance to try yet) .. you can do this:

;Test an array alement for a string value stored in $sTestValue
    If $sTestValue = "" & $aArrayItem[$nX]

HTH :ph34r:

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...