Jump to content

How to loop over a list of names


Go to solution Solved by inna,

Recommended Posts

Posted

Hello,

I see For...In...Next help which is this:

Local $aArray[4]

$aArray[0] = "a"
$aArray[1] = 0
$aArray[2] = 1.3434
$aArray[3] = "test"

Local $sString = ""
For $vElement In $aArray
    $sString = $sString & $vElement & @CRLF
    ConsoleWrite($sString)
Next

But what I'm trying to reach (and failed yet) is:

; the number of items on list or array changes and is not fixed (I don't know the count)

$list = ['me', 'you', 'him']

for $i in $list
    ConsoleWrite($i & @LF)
Next

; the console should print:
;me
;you
;him

How can I reach this?

I have a list of names and I should iterate over.

Thanks in advance

Posted
4 minutes ago, inna said:

Please compare my post to yours and compare the outputs.

 

I thought you had a problem because I couldn't figure it out

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
a
a
0
a
0
1.3434
a
0
1.3434
test
+>20:27:53 AutoIt3.exe ended.rc:0

 

for this reason it is placed in a series so that it makes some sense

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
$aArray[0]=a    $aArray[1]=0    $aArray[2]=1.3434   $aArray[3]=test 
+>20:35:35 AutoIt3.exe ended.rc:0

 

I know that I know nothing

Posted (edited)
2 hours ago, inna said:

But what I'm trying to reach (and failed yet) is:

$list = ['me', 'you', 'him']

for $i in $list
    ConsoleWrite($i & @LF)
Next

You have some syntax errors, which when you ran this the console should point you to the problem. Make sure that you read the errors that come up when you run your script. Here's the correct version:

Global $list[] = ['me', 'you', 'him']

For $i In $list
    ConsoleWrite($i & @LF)
Next

Mainly, you didn't initialize $list as an array with either $list[] = ['values'] or $list[3] (and with the scope, Global or Local).

However typically it's best to use For...To...Step...Next as you can then also get the index of the array. For...In...Next is best for objects or maps where there's not a sequential index.

Check out this page for some more tips on using AutoIt: https://www.autoitscript.com/wiki/Best_coding_practices

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...