Jump to content

Array works strange


Recommended Posts

Hi,

I was testing and trying to make an array, split it and use the output for something else.

Now i've made this script (see below)

Wanted to check trough $Array[0] (what shows u how much entries there are after splitting)

tried alot of combinations but dint work becouse it exceeds the array entries becouse of: $Array[$i+1].

Was getting crazy til i tried to use: if $test2 < $Array[0]+1

worked great till it exceeded the array entry and giving an error so i made and "extra entry" to try if it worked

but it worked strange becouse he was displaying all entries and after that an error becouse it exceeded the entry limit.

so i removed that one.

Next i placed a -1 (dunno why i did this, did/tried alot of thing) at: For $i = $Array[1] To $Array[0]-1

and now my script works great, it shows me the things i splitted without giving any errors.

my question is how is this possible?

can anyone explain it to me?

$List = "banaan-appel-noot-mies-extra entry"

$Array = StringSplit($List,'-') ;split $List at -


For $i = $Array[1] To $Array[0]-1 ;gives $i an array number (i putted a -1 here else the script doesnt work)
    
        $test2 = $Array[$i+1] ;counts +1 at the $i number
    
    if $test2 < $Array[0] Then ;if $test is below max array entry then give a message 

        MsgBox(0,"test array", $test2 ) ;at this place, comes the script where i am going to use the output
        
    EndIf

Next
Link to comment
Share on other sites

What's wrong is your usage or the loop (For) variable. It has to be numeric. Read again the help file for For.

Then , what is the purpose of your loop?

$test2 = $Array[$i+1] will assign a string to $test2, so it won't count anything.

Re-read the AutoIt datatypes and variables chapters of the helpfile.

Local $List = "banaan-appel-noot-mies-extra entry"
Local $Array = StringSplit($List, '-') ;split $List at -
For $i = 1 To $Array[0]
    ConsoleWrite($Array[$i] & @LF)
Next

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)

Link to comment
Share on other sites

there is nothing wrong with my script. it works great...

example of what i am going to do:

$List = "alcohol 120-fraps-everest-ftd-autoit v3"

$Array = StringSplit($List,'-') 


For $i = $Array[1] To $Array[0]-1 
    
        $test2 = $Array[$i+1] 
    
    if $test2 < $Array[0] Then 

        RunWait($test2 &"\Installer.exe") ;this will be the scrip i gonna use on this place
        
    EndIf

Next
Link to comment
Share on other sites

It only works by chance and you really should listen to advises.

For $i = $Array[1] ... This part is already wrong. For deals with a numeric variable, so it converts $Array[1] to a number. $Array[1] contains "alcohol 120" and Number() return 0. So you're doing:

For $i = 0 ... but the array returned by StringSplit contains the number of split entries. Hence you're going to process $Array[0], which in your particular case equals 5...

Then, the last element in $Array is $Array[5] and contains "autoit v3", but since you stop the loop at $Array[0]-1 (equals 4), you're only process this entry as you process element $i+1, once assigned (uselessly) to $test2.

That's not all: the test If $test2 < $Array[0] is always true: $test2 is a substring and $Array[0] a number, so AutoIt does: If Number($test2) < 5, which is: If 0 < 5, which is always true.

All in all, you've accumulated errors which more or less compensate, but that's only by chance.

Your script should be:

$List = "alcohol 120-fraps-everest-ftd-autoit v3"

$Array = StringSplit($List,'-')

For $i = 1 To $Array[0]
    RunWait($Array[$i] & "\Installer.exe") ;this will be the script I gonna use on this place
Next

If you want/need to produce more complex scripts, then you definitely should read a good array tutorial. Use the search feature, there are several excellent ones, you need 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)

Link to comment
Share on other sites

It only works by chance and you really should listen to advises.

i do and wanna do else i dint ask for it :idea:

in the beginning i dont know how to call the array,

when i putted an msg box in it it only showed me numbers.

thats why i made something like this script.

the key is $Array[$i], but i dint know how to get the entries out of the array

now i know how to call the enrties the right way with $Array[$i].

And offcourse this will make things alot easier for me now :)

For $i = $Array[1] ... This part is already wrong. For deals with a numeric variable, so it converts $Array[1] to a number. $Array[1] contains "alcohol 120" and Number() return 0

For $i = $Array[1] To $Array[0] is not wrong (think its not)

why?

u cal the array with:

$Array[1] will show u alcohol 120 etc...

$Array[2] ...

$Array[3] ...

$Array[4] ...

$Array[5] ...

so if u only need lets say the 3th split then i can call it like

$Array[3] then it shows me everest

so if i use: For $i = $Array[1] To $Array[0]

it wil be the same as: For $i = 1 To $Array[0]

or am i wrong here?

Link to comment
Share on other sites

so if i use: For $i = $Array[1] To $Array[0]

it wil be the same as: For $i = 1 To $Array[0]

Not at all.

$Array[1] is text, as explained earlier. For expect a number, so it convert your text to a number,giving 0. But you don't want to start indexing at 0, since $Arrray[0] contains the number of text elements in the rest of the array..

For will not iterate thru the contents of the array, it will iterate a numeric variable (integer in most cases) which you use to access the array elements by indexing.

Please make yourself a favor and read helpfile datatypes and variables chapter, then read the rest!

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)

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