Jump to content

Using 2 for loops ?


LerN
 Share

Recommended Posts

Is that possible to use 2 for loops at the same time ?
i don't think so, i have tried something like that but didn't work at all ...
 

Local $loop

$read = FileReadToArray($filename1)
$read2 = FileReadToArray($filename2)

For $i2 = 1 to ubound($read2) - 1
For $i3 = 1 to UBound($readdate) - 1

$loop &= $read[$i] & $read2[$i3]
msgbox(0,"",$loop)

Next
Next

^ this code will work at the first time but the second time it will only read from second for loop 
so do u have any idea ?

Link to comment
Share on other sites

Just now, Kaworu said:

Hi LerN, 

Not sure what you want to do, but you have undeclared variables like $i, $readdate. 

Local $loop

$read = FileReadToArray($filename1)
$read2 = FileReadToArray($filename2)

For $i = 1 to ubound($read) - 1
    For $j = 1 to UBound($read2) - 1

        $loop &= $read[$i] & $read2[$j]
        msgbox(0,"",$loop)

Next
Next

 

That isn't the problem actually when u make loops like ^ first messagebox will read everything fine in the 2 files but the second messagebox will read the same for $read but it will update $read2 to the second line the in txt file that's the problem :)

Link to comment
Share on other sites

Understand that the second (internal loop) has to be completed (looped through entirely) on each iteration of the first loop. So if you were to read letters during the first loop, and numbers during the second loop; then the output might look something like this: A1, A2, A3 ==> B1, B2, B3 ==> C1, C2, C3 etc...

It sounds to me that you want it to work differently: A1, B2, C3 etc... but that's not how it works. :rolleyes:

Edited by czardas
Link to comment
Share on other sites

Try something like:

Local $sFileName1 = @ScriptDir & "\Filename1.txt"
Local $sFileName2 = @ScriptDir & "\Filename2.txt"
Local $aRead1 = FileReadToArray($sFileName1)
Local $aRead2 = FileReadToArray($sFileName2)
;~ Assign smallest array count to $iRead
Local $iRead = UBound($aRead1) > UBound($aRead2) ? UBound($aRead2) - 1 : UBound($aRead1) - 1
For $i = 0 To $iRead
    ConsoleWrite($aRead1[$i] & $aRead2[$i] & @CRLF)
Next

 

Link to comment
Share on other sites

Thank you mikell !
but could you tell me if i can use more than ? and more than : ... ?
something like that :
 

Local $sFileName1 = @ScriptDir & "\Filename1.txt"
Local $sFileName2 = @ScriptDir & "\Filename2.txt"
Local $sFileName3 = @ScriptDir & "\Filename3.txt"
Local $sFileName4 = @ScriptDir & "\Filename4.txt"
Local $aRead1 = FileReadToArray($sFileName1)
Local $aRead2 = FileReadToArray($sFileName2)
Local $aRead3 = FileReadToArray($sFileName3)
Local $aRead4 = FileReadToArray($sFileName4)
;~ Assign smallest array count to $iRead
Local $iRead = UBound($aRead1) > UBound($aRead2) > ubound($aread3) > ubound($aread4) ? UBound($aRead2) - 1 : UBound($aRead1) - 1
For $i = 0 To $iRead
    ConsoleWrite($aRead1[$i] & $aRead2[$i] & @CRLF)
Next

could you tell me how can i do that ? :)

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