Jump to content

Applying A Task To All Items In An Array


amin84
 Share

Recommended Posts

Hi, The code below is very simple but yet I don't know what I'm doing wrong. I just generated an array and I want to do a task (ConsoleWrite in this case) on every item in the array. I though using a for loop will do the job but I guess not. So what am I doing wrong here?

Tnx.

#include <Array.au3>

Global $avArray[10]

$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

For $i = 1 To $avArray[0]
    ConsoleWrite($avArray[$i]&@LF)
Next
Link to comment
Share on other sites

You are assuming that the first element of the array contains the number of elements. This is not the case by default, but the value is filled by many UDFs. It's up to you, how you want to handle a certain array, if it does not contain a counter in the first element fall-back to ubound.

#include <Array.au3>

Global $avArray[10]

$avArray[0] = 9
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

For $i = 1 To $avArray[0]
    ConsoleWrite($avArray[$i]&@LF)
Next

ConsoleWrite(@crlf & @crlf)
Global $avArray2[10]

$avArray2[0] = "JPM"
$avArray2[1] = "Holger"
$avArray2[2] = "Jon"
$avArray2[3] = "Larry"
$avArray2[4] = "Jeremy"
$avArray2[5] = "Valik"
$avArray2[6] = "Cyberslug"
$avArray2[7] = "Nutster"
$avArray2[8] = "JdeB"
$avArray2[9] = "Tylo"

For $i = 0 To UBound($avArray2)-1
    ConsoleWrite($avArray2[$i]&@LF)
Next
Edited by KaFu
Link to comment
Share on other sites

$array[0] was not a count of the array it was a string value so you need to use Ubound() to get the size of the array.

#include <Array.au3>

Global $avArray[10]

$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

For $i = 0 To Ubound($avArray) -1
    ConsoleWrite($avArray[$i]&@LF)
Next
Link to comment
Share on other sites

Even better, autoit kinda but not really supports something like iterators:

global $avArray[10] = ["JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]

$data = ""

for $i in $avArray
    consolewrite($i & @CRLF)
next

If autoit didn't suck, you would also be able to do something like this:

for $x in [$i & @CRLF | $i <- $avArray]
    consolewrite($x)
next

or maybe:

for $i in map((\x -> $x & @crlf), $avArray)
    consolewrite($i)
next

Also, question:

This does not write to a DOS console unless the script is compiled as a console application..

Seriously? Pain in the nuts to have to compile. No other language interpreter has a problem with this. I suppose it's easy enough with scite. I generally write in vim and run in virtualbox through samba. Edited by Smorg
Link to comment
Share on other sites

Another way (though I'm guessing autoit probably doesn't do TCO, and the implementation being a C array rather than linked list probably makes this inefficient):

#include <Array.au3>
global $avArray[10] = ["JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]

func doitmofo($x)
    if ubound($x) = 0 then
        return $x
    else
        return _ArrayPop($x) & @CRLF & doitmofo($x)
    endif
endfunc

msgbox(0, "", doitmofo($avArray))
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...