LerN

Using 2 for loops ?

12 posts in this topic

#1 ·  Posted

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 ?

Share this post


Link to post
Share on other sites



#2 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

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 :)

Share this post


Link to post
Share on other sites

#4 ·  Posted

Also yea i was wrong in using $readdate it must be $read2

Share this post


Link to post
Share on other sites

#5 ·  Posted

So could you please explain what would you like to achieve? What do you want msgbox to display when for loops through the array.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

Thank you Subz !
but could you please explain ? and : expression cuz i didn't understand it from the help file :)

Share this post


Link to post
Share on other sites

#9 ·  Posted

and what if i wanted to add more files ?

Share this post


Link to post
Share on other sites

#10 ·  Posted

56 minutes ago, LerN said:

please explain ? and : expression

Ternary operator  :)
result = (is this condition true) ? (if yes return this) : (if no return that)

Share this post


Link to post
Share on other sites

#11 ·  Posted

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 ? :)

Share this post


Link to post
Share on other sites

#12 ·  Posted

Okay i have solved my problem by trying thanks all :)

Share this post


Link to post
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