Paulie Posted April 6, 2007 Share Posted April 6, 2007 (edited) 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 April 6, 2007 by Paulie Link to comment Share on other sites More sharing options...
Developers Jos Posted April 6, 2007 Developers Share Posted April 6, 2007 (edited) 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 April 6, 2007 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 More sharing options...
Valuater Posted April 6, 2007 Share Posted April 6, 2007 This is a s close as i could guess that you would want to do Local $a[100][11] For $i = 1 to 3 For $j = 1 to 10 MsgBox(0,"test #" & $i & "-" & $j, $a[$i][$j], 1) Next Next in this example an array cannot be treated the same as a "single" variable 8) Link to comment Share on other sites More sharing options...
Paulie Posted April 6, 2007 Author Share Posted April 6, 2007 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 More sharing options...
Valuater Posted April 6, 2007 Share Posted April 6, 2007 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 8) Link to comment Share on other sites More sharing options...
Paulie Posted April 6, 2007 Author Share Posted April 6, 2007 (edited) 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 April 6, 2007 by Paulie Link to comment Share on other sites More sharing options...
Valuater Posted April 6, 2007 Share Posted April 6, 2007 Sorry Paulie, I may not be able to explain to you in the direction that you could clearly relate to... However, I am sure my previous answer does what you want to do... maybe you can study/play with it a little more 8) Link to comment Share on other sites More sharing options...
therks Posted April 7, 2007 Share Posted April 7, 2007 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 ??? My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 7, 2007 Share Posted April 7, 2007 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 More sharing options...
Paulie Posted April 7, 2007 Author Share Posted April 7, 2007 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 More sharing options...
therks Posted April 7, 2007 Share Posted April 7, 2007 I'm afraid I still don't understand what you're trying to do. As near as I can see, the code I gave will replicate all the examples you gave. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Uten Posted April 7, 2007 Share Posted April 7, 2007 (edited) 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 recursedIf 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.. Edited April 7, 2007 by Uten Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
therks Posted April 11, 2007 Share Posted April 11, 2007 Dang. I was hoping for a reply from Paulie. I'm quite curious about this whole thing now. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Paulie Posted April 11, 2007 Author Share Posted April 11, 2007 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 More sharing options...
therks Posted April 11, 2007 Share Posted April 11, 2007 (edited) 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 April 11, 2007 by Saunders My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Paulie Posted April 12, 2007 Author Share Posted April 12, 2007 (edited) 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 April 12, 2007 by Paulie Link to comment Share on other sites More sharing options...
therks Posted April 14, 2007 Share Posted April 14, 2007 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. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
PsaltyDS Posted April 14, 2007 Share Posted April 14, 2007 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! 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 More sharing options...
Teldin Posted April 14, 2007 Share Posted April 14, 2007 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 More sharing options...
therks Posted April 15, 2007 Share Posted April 15, 2007 There you go with your tricky If/Next loops again! Why are you solutions always so... complicated! D'oh! Guess you can tell I didn't test that code before I posted. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now