Jump to content

For In loop to iterate two arrays simultaneously


Recommended Posts

Is there a way to iterate two arrays with in a For...In loop as mentioned below?

Tried different syntaxes, but nothing works...

Local $aArray[2]
$aArray[0] = "aaa"
$aArray[1] = "bbb"

Local $bArray[2]
$bArray[0] = "ccc"
$bArray[1] = "ddd"

For [ $element1 $element2 ] In $aArray $bArray
ConsoleWrite($element1 & $element2)
Next
Edited by sivaramanm
Link to comment
Share on other sites

You can't do it that way, you might be able to do it with a nested for loop inside the other for loop, but you can't do it with 2 arrays like the way you tried.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Something like this? (not exactly sure what you are trying to output)

Local $aArray[2]
 $aArray[0] = "aaa"
 $aArray[1] = "bbb"
Local $bArray[2]
 $bArray[0] = "ccc"
 $bArray[1] = "ddd"
If UBound($aArray) = UBound($bArray) Then
 For $i = 0 To UBound($aArray) - 1
    ConsoleWrite($aArray[$i] & " " & $bArray[$i] & @CRLF)
  Next
EndIf

output:

aaa ccc

bbb ddd

not exactly what you asked for (for...in loop) put a workaround

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

sivaranamn,

Here's a couple of routines that may interest you.

local $arr1[5] = ['a1','a2','a3','a4','a5']
local $arr2[3] = ['b1','b2','b3']
local $arr3[9] = ['c1','c2','c3','c4','c5','c6','c7','c8','c9']

; find largest of the three arrays

local $max
$max = ubound($arr1)
if ubound($arr2) > $max then $max = ubound($arr2)
if ubound($arr3) > $max then $max = ubound($arr3)

; redim all arrays to the match the size of the largest

redim $arr1[$max]
redim $arr2[$max]
redim $arr3[$max]

; display the values of the arrays

consolewrite(stringformat('        %8s %8s %8s','$arr1','$arr2','$arr3') & @lf)
ConsoleWrite(@LF)
for $1 = 0 to $max - 1
    consolewrite(stringformat('CELL[' & $1 & '] %8s %8s %8s',$arr1[$1],$arr2[$1],$arr3[$1]) & @lf)
next

#include <array.au3>

local $a1[20] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $a2[10] = [1,2,3,4,5,6,7,8,9,0]
local $a3[50] = ['','',3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $a4[5] = [1,2,3,4,5]
local $a5[7] = [1,2,3,4,5,6,7]

local $a10 = _array_combine($a1,$a2,$a3,$a4,$a5)

_arraydisplay($a10)

func _array_combine($arr1,$arr2,$arr3=0,$arr4=0,$arr5=0,$arr6=0,$arr7=0,$arr8=0,$arr9=0,$arr10=0)

    ;-----------------------------------------------------------------------------------------------------------------
    ; combine 2 to 10 array into 1 2D array
    ;-----------------------------------------------------------------------------------------------------------------

    ; ensure at least two arrays
    if not IsArray($arr1) or not IsArray($arr2) then return seterror(1,0,0)

    ; find # of array passed ($1 = # of arrays)
    for $1 = 1 to 10
        if not isarray(eval("arr" & $1)) then ExitLoop
    Next

    ; set # of cols variable to # of arrays in parameter
    local $num_cols = $1 - 1
    local $max_elements = 0

    ; set # elements to largest array passed
    for $1 = 1 to $num_cols
        if ubound(eval("arr" & $1)) - 1 > $max_elements then $max_elements = ubound(eval("arr" & $1))
    next

    ConsoleWrite('Formatting target array as ' & $num_cols & ' columns and ' & $max_elements & ' elements.' & @LF)

    ; define result array
    local $aTarget[$max_elements][$num_cols], $aTemp

    ; populate result array
    for $1 = 0 to $num_cols -1
        $aTemp = eval("arr" & $1+1)
        for $2 = 0 to ubound($aTemp) - 1
            $aTarget[$2][$1] = $aTemp[$2]
        Next
    next

    return $aTarget

endfunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • 3 weeks later...

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