Jump to content

multiple vars with loopstatement


Recommended Posts

This may be confusing for me to explain with words, but the script may speak for itself.

for $i = 1 to 5
Assign(eval("$oExcel"&$i),_ExcelBookOpen($excelFileLocation&$excelFileName, 1, 1,3,0),1)
_ExcelBookClose(eval("$oExcel"&$i, 0, 0)
next

What I learned:

1) $ cannot be used in the eval when referencing a variable (PsaltyDS)

2) execute and not eval needs to be used to call the return of the assigned data (see help manual on eval returns)

3) use array instead for the above method (see PsaltyDS)

Better Answer

Please refer to PsaltyDS

Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

I understand that using eval in AUI3 doesn't work the same as it does in other languages. I know it adds vars and doesn't really do what I'm trying to do, but I had to use something to explain what I'm attempting.

A decision is a powerful thing
Link to comment
Share on other sites

This may be confusing for me to explain with words, but the script may speak for itself.

for $i = 1 to 5
Assign(eval("$oExcel"&$i),_ExcelBookOpen($excelFileLocation&$excelFileName, 1, 1,3,0),1)
_ExcelBookClose(eval("$oExcel"&$i, 0, 0)
next

: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

sorry for the type-o. I was only doing one arguement before (ie

_ExcelBookClose(eval("$oExcel"&$i),0,0)
)

that still doesn't work though, because eval Returns the value of a variable defined by a string. Thus, I can't use eval. I just don't know what to use in its stead.

A decision is a powerful thing
Link to comment
Share on other sites

Let me reword this.

I want to set a new variable using a loopstatement.

for $i = 1 to 5
  Global $var&$i = $i ;<---- this should create 5 variables (ie $var1,$var2,$var3,etc.)
  MsgBox(0,'',$var&$i)
next

That's completely improper syntax and will not compile. However, hopefully this will give everyone a clear understanding of what I'm trying to do.

I know this wasn't phrased clear to begin with; my apologies.

Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

Overall answer

for $i = 1 to 5
        Assign("var"&$i,$i,2)
        MsgBox(0,'',execute("$var"&$i))
        
        Assign("oExcel"&$i, _ExcelBookOpen($excelFileLocation&"list"&$i&".csv", 1, 1,3,0),1)
        MsgBox(0,'',execute("$oExcel"&$i))
        _ExcelBookClose(execute("$oExcel"&$i), 0, 0)
next

What I learned:

1) $ cannot be used in the eval when referencing a variable (PsaltyDS)

2) execute and not eval needs to be used to call the return of the assigned data (see help manual on eval returns)

3) use array instead for the above method (see PsaltyDS)

Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

...What I learned:

1) Eval cannot be used in the first parameter of Assign

2) execute and not eval needs to be used to call the return of the assigned data

Actually Eval() works fine inside a call to Assign(). I put in another typo myself that lead you astray. When calling a variable name in Eval() DON'T INCLUDE THE $ IN THE NAME. The dollar sign is a flag to the AutoIT interpreter that what follows is a variable name, it is not exactly part of the variable name itself, so it should not be included in the string name given to Eval(). Simplified demo:

For $i = 1 To 5
    Assign("var" & $i, $i)
    Assign("oExcel" & $i, "SimulatedExcelObject_" & Eval("var" & $i))
    MsgBox(64, 'Results', "oExcel" & $i & " = " & Eval("oExcel" & $i))
NextoÝ÷ ØÚ0¢{h¶ì¢[­ð'!Ëhv+Øb°, ÿö¥[æ§vW®{hºÇ®¶²³}Ø¢êìr¸©¶Ì"VÞrWê梷«zXnWjÈ®Ú+y§Z×^nèØî²Ù¨­ç¥z§¶'ò¢çbµÈmà,²('üKÚØZè®è¥êÚ®¶²±IÝz»-jwmçèZ0xzjܨº»®*mx%÷°Y[zk)©®ÞtÚ-zØZ¶Øb²èqë,q©ìzWÚv;¬¶Ú÷Þ­éíéජ»ayªëk+(Ê¢}ý¶¯zÚ!j·¡×r§µú+¶¨n7¶Æ®¶­sb4æ6ÇVFRfÇC¶'&æS2fwC° ¢b33c´6÷VçBÒçWD&÷gV÷C´'&FVÖògV÷C²ÂgV÷C´÷rÖçö&¦V7G2Fò7&VFRÓògV÷C²¤bb33c´6÷VçBfÇC²÷"b33c´6÷VçBfwC²FVâW@ ¤vÆö&Âb33c¶dW6VÄö&¥³ÒҳФf÷"b33c¶ÒFòb33c´6÷Vç@ ô'&FBb33c¶dW6VÄö&¢ÂgV÷Cµ6×VÆFVDW6VÄö&¦V7EògV÷C²fײb33c¶¤æW@¢b33c¶dW6VÄö&¥³ÒÒT&÷VæBb33c¶dW6VÄö&¢Ò ¥ô'&F7Æb33c¶dW6VÄö&¢ÂgV÷Cµ&W7VÇG2gV÷C²

: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

PsaltyDS, thank you so, so very much! I didn't realize that the $ was not required for the Eval! I honestly didn't know how it was going to handle the $ sign, but I couldn't find any info (thus part of my much thank you). That sort of explanation is gold for me!

As for the usage of arrays, all the more of a thank you and that's like a ton of gold. :shocked: I agree that it is the REAL solution to what I was doing (though at the time I was trying to understand eval and assign when I started this thread). I way prefer Arrays for the very reasons you stated (ie "cleaner, more reliable, easier to read and debug, and just more elegant...process can self adjust to different lengths"). I didn't even think of using arrays for this purpose, and I really like ditching the assign and eval and using the array like you said!

Thank you so very much for the explanation, stern opinion, and demo scripts!!! This is like a learning gold mine. I really appreciate the well laid out reply at that too (good wording and organization).

A decision is a powerful thing
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...