Jump to content

Arrays in For...Next Loops


Recommended Posts

Why do I get a syntax error when I try to use an array in a For..Next loop

Ex:

Local $a[100]
For $i = 1 to 3
    For $a[$i] = 1 to 10
        MsgBox(0,"test",$a[$i])
    Next
Next

Is there any reason that this doesn't work and give me this:

: ERROR: syntax error
        For $a[
        ~~~~~~^

Helpfile never specifies against using arrays...

Edited by Paulie
Link to comment
Share on other sites

  • Developers

The Var used in a For..Next loop is implicitly dimmed and cannot be an Array "cell" but needs to be a regular variable name.

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The Var used in a For..Next loop in implicitly dimmed and cannot be an Array "cell" but needs to be a regular variable name.

Then how can you loop nested loops?

By that i mean:

If I could use arrays, I could nest several for loops inside another

So it could equate to going:

For $a= 1 to 5
    For $1 = 1 to 5
        ;bla
    Next
    For $2 = 1 to 5
        ;bla
    Next
    For $3 = 1 to 5
        ;bla
    Next
    For $4 = 1 to 5
        ;bla
    Next
    For $5 = 1 to 5
        ;bla
    Next
NextoÝ÷ ØLz[z)ài¹^.+n¦X¤y«­¢+Ù½ÈÀÌØíôÄѼÔ(%½ÈÀÌØílÀÌØí¥tôÄѼÔ($$í±(%9áÐ)9áÐ

Which would eliminate having to know how many nested loops you are going to have.

This would be a useful feature personally.

Any idea on how to do it?

Link to comment
Share on other sites

Maybe you are looking for UBound()

$Week = StringSplit("1234", "")
$Days = StringSplit("Sun,Mon,Tues,Wed,Thur,Fri,Sat", ",")
For $i = 1 to UBound($Week) -1
    For $j = 1 to UBound($Days) -1
        MsgBox(0,"week #" & $i, $Days[$j], 1)
    Next
Next
oÝ÷ Ûú®¢×)^"¯zëu@hºw[yú+xýz-²êÞ"µÆ§éiè^­àZ²'ç¶î§è®w±¶Z(¥ú+צv¯j¸n¬7±êâzÊצ±*,k&¬jw±jje{¡j÷±êâzÇ(Ö¢)àªê-j«¨¶«¨µº®¢Ö§v«¨µÊ®¢ÛajÛ-·×¦²Ê ¢)àm§$¶aDZjjezë"
ܪÚr׫±«­¢+ØìQ¡¥Ì¹½Üݽձ̱½½ÁÌ¥¹ÍѽÔ(½ÈÀÌØìÄôÄѼÔ(í±(9áÐ(½ÈÀÌØìÈôÄѼÔ(í±(9áÐ(½ÈÀÌØìÌôÄѼÔ(í±(9áÐ

and this would work fine just like that. Except now, I want to do it for "a","b",c",and"d"

Meaning that now, I would need 4 For...Next loops.

How can I account for a variable number of for loops?

Edited by Paulie
Link to comment
Share on other sites

I see what you are trying to do Paulie, and I don't understand why it wouldn't work. An array "cell" should be a valid l-value. L-value being a value at the left hand side of an equals sign, meaning it can be set.

I think you should be able to do things like that, but I have also run into problems of trying to read an array value from a function. Like DllCall(...)[0]. Arrays are complicated and I guess not easy to fully implement.

Link to comment
Share on other sites

Local $a[100]
For $i = 1 to 3
    For $ii = 1 to UBound($a) - 1
        For $iii = 1 to 10
            $a[$ii] = $iii
            MsgBox(0,"test",$a[$ii])
        Next
    Next
Next

???

that still requires you to have a variable in the loop though

(the $iii)

I don't know how many loops there will be, so i don't have the abiltiy to designate a variable unless i can name it something that can be recursed

Link to comment
Share on other sites

Basically, I need to run a for..next loop for EACH item in a variably sized series of items.

...

I think your stuck in your own mindset. Adding to that your first sample, where you use the array item as an identifier for the loop, will destroy the value of that item.

If that is what you want this code should work for you:

Local $arr[5] = ["a", "b", "c", "d", "e"]
local $i, $j
For $i = 0 to UBound($arr) - 1
    For $j = 1 to 5
         ;Do something
    Next
    ;TODO: Don't think you want this but that is basically what you did in your first sample
    $arr[$i] = $j
Next

that still requires you to have a variable in the loop though

(the $iii)

I don't know how many loops there will be, so i don't have the abiltiy to designate a variable unless i can name it something that can be recursed

If you need a variable you need a variable. Why do you want to avoid them?

Take a look at the arrays tutorial and see if it can be useful to you..:shocked:

Edited by Uten
Link to comment
Share on other sites

Dang. I was hoping for a reply from Paulie. I'm quite curious about this whole thing now.

Oh, well I had kind of given up on what i was trying to accomplish. BUt I will try to elabore more on my question.

Basically, I was trying to loop a variable number of loops in another loop.

Because when you nest 1 loop it looks like this:

For $a = 1 to 10; "a" loop
     For $b = 1 to 10; "b" loop
          ;foo
     Next
Next
oÝ÷ ح­ü¨»§µçfº[b¦Wªº[ªê-)±»­¢yrÖ«¨µª®¢Ùh¢¨
.קvêpØZ))¶¬jëh×6
For $a = 1 to 10
     For $b1 = 1 to 10
     ;foo1
     Next
     For $b2 = 1 to 10
     ;foo2
     Next
     For $b3 = 1 to 10
     ;foo3
     Next
Next
oÝ÷ Ø­#§µçm¢ëjºÚÊË(¶­Ê.ØZ½æ§w^~)âµéî·«¡öå¢lo+"rë-x"·Y¨­ëb殶­s`¤f÷"b33c¶ÒFò¢f÷"b33c¶çBÒFò²FRæWvÇFFVBæW7BÆö÷¢f÷"b33c¶%²b33c¶çEÒÒFò¢¶föóÓ¢æW@¢æW@¤æW@

maybe that was a little clearer

Link to comment
Share on other sites

See, to me that still says that the "b loop" is going to loop 10 times (in that example), right? I'm not sure why adding a substitute variable in the interim is a problem. What would you be putting in the $b array? Anything?

I think what I don't understand, is why this doesn't work...

(From your example)

For $a = 1 to 10
     For $int = 1 to 10 ; the newly added nest loop
          For $temp = 1 to 10
              $b[$int] = $temp
          ;foo1-10
          Next
     Next
Next

*Edit: Of course, you'll have to declare the $b array before the for loop, but you know that already, right?

Edited by Saunders
Link to comment
Share on other sites

See, to me that still says that the "b loop" is going to loop 10 times (in that example), right? I'm not sure why adding a substitute variable in the interim is a problem. What would you be putting in the $b array? Anything?

I think what I don't understand, is why this doesn't work...

(From your example)

For $a = 1 to 10
     For $int = 1 to 10 ; the newly added nest loop
          For $temp = 1 to 10
              $b[$int] = $temp
          ;foo1-10
          Next
     Next
Next

*Edit: Of course, you'll have to declare the $b array before the for loop, but you know that already, right?

Can you use one variable in multiple loops? ($temp)

EDIT: *Tests* wow, I guess you can... Why don't I know this?

Well thanks, maybe this will let me finally figure this problem out.

Edited by Paulie
Link to comment
Share on other sites

Ahhh, so that's where the confusion was. Don't you hate when we make assumptions and get hung up on them? I don't know how many times it took me forever to debug, or I wrote longer less efficient code just because I didn't try something I figured wouldn't work... when it actually would!

But yeah, when you use a variable in a for loop, it gets redefined. So if you go:

$number = 100000
If $number = 1 to 10
  ToolTip($number)
Next

It still counts from 1 to 10. Also, after that loop, $number would actually = 11, which I thought was kinda strange.

Link to comment
Share on other sites

Ahhh, so that's where the confusion was. Don't you hate when we make assumptions and get hung up on them? I don't know how many times it took me forever to debug, or I wrote longer less efficient code just because I didn't try something I figured wouldn't work... when it actually would!

But yeah, when you use a variable in a for loop, it gets redefined. So if you go:

$number = 100000
If $number = 1 to 10
  ToolTip($number)
Next

It still counts from 1 to 10. Also, after that loop, $number would actually = 11, which I thought was kinda strange.

There you go with your tricky If/Next loops again! Why are you solutions always so... complicated!

:):P;):sorcerer::cheer::(:(:shocked:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I could be way off here but I think I just did something you are trying to do Paulie:

$m = 0
        $u = 0
        While $m  < Ubound($myArray2) - 2
            
        While $u <  Ubound($myArray2)*(Ubound($myArray2)-3)
        ;I'm comparing elements of an array with every other element of that array. 
        
             For $q = 0 to Ubound($myArray2) - 3
                ;comparing these elements until I get to the end then the previous loop will move to the next element. The output is created here also
                $u = $u + 1
            Next
        $m = $m + 1 
     WEnd       
 WEnd

It's probably ugly to you guys but it works good for me.

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