Jump to content

Dynamically create variables?


Sunaj
 Share

Recommended Posts

Hi, I've been trying to do dynamic initialization of variables with autoit.. like so:

....
$i = 1
    While $i <= UBound($SomeArray) - 1
        $Menu & $i = GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)
        GUICtrlSetOnEvent ($Menu & $i,"Hotkey" & $i)
        $i = $i + 1
    WEnd
....

This is obviously not working - but are there any hacks that could achieve dynamic variable initiation?

Link to comment
Share on other sites

Hi, I've been trying to do dynamic initialization of variables with autoit.. like so:

....
$i = 1
    While $i <= UBound($SomeArray) - 1
        $Menu & $i = GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)
        GUICtrlSetOnEvent ($Menu & $i,"Hotkey" & $i)
        $i = $i + 1
    WEnd
....

This is obviously not working - but are there any hacks that could achieve dynamic variable initiation?

make $Menu an array.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Link to comment
Share on other sites

dim $Menu[1]
$i = 1
    While $i <= UBound($SomeArray) - 1
        $Menu[$i - 1] = GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)
        GUICtrlSetOnEvent ($Menu[$i - 1],"Hotkey" & $i)
        $i = $i + 1
    ReDim $Menu[$i]
    WEnd

Does that work?

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi, I've been trying to do dynamic initialization of variables with autoit.. like so:

....
$i = 1
    While $i <= UBound($SomeArray) - 1
        $Menu & $i = GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)
        GUICtrlSetOnEvent ($Menu & $i,"Hotkey" & $i)
        $i = $i + 1
    WEnd
....
oÝ÷ Ù8b²+(nø¨ºÉrp¢¹"޶«y©ò§$²ØZµÊ.Ö'¯yܧjh½ªâi¹^x­«b¢êº^bjw+y«^½ªâi¹^³­v¦zÇ+y«^v)춸§^­«b¢{-ç(f§vÆ«x,   ÚÑ/jPn¶)ÚÉæ«¢{aÇè®éÙ*h²Û(Øb²Ú)ÉènrÉëajÇ(ë©Â+aj
(v·²ß¢·hx-+"Ë^iÚjv«­¬*.«Þmæ«çZ®¶²³'­v("Ü"Yç¯z¼­)àÊ£­X¬¶Êu«Zy«"z¶§vj+zËZnW¢Ø^~ën­àèÛaÌË­©Ú®¶²jëh×6; Include statement at the top of your script
#include <array.au3>

Dim $aMenu[1]
For $i = 1 To UBound($SomeArray) - 1
    _ArrayAdd($aMenu, GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext))
    GUICtrlSetOnEvent ($aMenu[$i] ,"Hotkey" & $i)
Next
$aMenu[0] = Ubound($aMenu) - 1oÝ÷ Øíz»ay¦è½ç(uë'^¶»§³*.«Þjv«­¬¨~ö«¦åzW§Øm«¢{Z{-à­®R²íéè­zk4ߦz{´ríj)춢éí¡ûhµ©fz{¢µé¬âÓ~1éîÖØk»Múhǧº)ÿÊ'µ¨§¶¢{k¢R°Z+y§!éîצjw¯z{_ºw-è|z-쵶îd{(§þ(Z±·Ç­N,yû§rبÌ"XZ½ëhmç^rV«yÜ!Èjyèµ·¢w¢©h¢(ʦmÁ©í¶Þ¶§ØZ¶Ëkj× É.r¥v"Ûazf¥Ú(ãâ-zgîËb¢v§v+pYd0'!¢w°jËç-yÖò¶PD²§+¡«­¢+Øì%¹±ÕÍÑѵ¹ÐÐѡѽÀ½å½ÕÈÍÉ¥ÁÐ(¥¹±Õ±ÐíÉÉä¹ÔÌÐì((ìQ¡¥ÉÍÐÁÉнå½ÕÈÍÉ¥Áи¸¸()¥´ÀÌØí5¹ÕlÅt)½ÈÀÌØí¤ôÄQ¼U   ½Õ¹ ÀÌØíM½µÉÉ䤴Ä(%}ÉÉå ÀÌØí5¹Ô°U%
Ñɱ
ÉÑ5¹Õ%Ñ´ ÀÌØíM½µÉÉålÀÌØí¥t°ÀÌØí=ÁÑ¥½¹Í
½¹ÑáФ($ì
¡¹Ñ¡½±±½Ý¥¹±¥¹¥¹Í¥Ñ¡½È±½½À¸¸¸(%U%
ÑɱMÑ=¹Ù¹Ð ÀÌØí5¹ÕlÀÌØí¥t°ÅÕ½Ðí}5¹Õ%Ñ´ÅÕ½ÐìµÀìÀÌØí¤¤)9áÐ(ÀÌØí5¹ÕlÁtôU½Õ¹ ÀÌØí5¹Ô¤´Ä((ìQ¡ÉÍнå½ÕÈÍÉ¥Áи¸¸((ì1½°Õ¹Ñ¥½¸½Èͱѥ¹µ¹Ô¥Ñ´)Õ¹}5¹Õ%Ñ´ ¤(%1½°ÀÌØí¤°ÀÌØí
Ñɱ9Õ´ôU%}
QI1%(%½ÈÀÌØí¤ôÄQ¼ÀÌØí5¹ÕlÁt($%%ÀÌØí5¹ÕlÀÌØí¥tôÀÌØí
Ñɱ9Õ´Q¡¸($$%5Í ½à ØаÅÕ½Ðí5¹Õ%Ñ´ÅÕ½Ðì°ÅÕ½Ðíe½Ôͱѵ¹Ô¥Ñ´¹¼¸ÅÕ½ÐìµÀìÀÌØí¤µÀìÅÕ½Ðì°Ý¥Ñ ½¹Ñɽ°%ÅÕ½ÐìµÀìÀÌØí
Ñɱ9Õ´µÀìÅÕ½Ðì¸ÅÕ½Ðì¤($$%IÑÕɸ($%¹%(%9áÐ)¹Õ¹

Cheers!

:lmao:

Edit: Replaced missing close-paren per Sunaj.

Edited by PsaltyDS
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

Hi PsaltyDS; this looks very promising - also easier to understand the code in this way..! I am already using arrays f a lot of other data in my prg but just had trouble when it came to using them for GUICtrlCreate's (the way I did it caused errors..etc). Well, with the code below I can see that those are troubles of the past - will do implementation of this new code tomorrow. Will report back on progress, very good and easy to grasp posting you made. Thanks :lmao:

You can create variables with names created in string operations, the commands are Assign() and Eval(). But in a year on this forum, and many posts on this topic, nobody yet has come up with a good reason for doing this instead of an array.

If you haven't learned arrays yet, do so. It will make everything you do with lists of data easier and more stable in the future. Do it this way with an array:

; Include statement at the top of your script
#include <array.au3>

Dim $aMenu[1]
For $i = 1 To UBound($SomeArray) - 1
    _ArrayAdd($aMenu, GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)
    GUICtrlSetOnEvent ($aMenu[$i] ,"Hotkey" & $i)
Next
$aMenu[0] = Ubound($aMenu) - 1oÝ÷ Øíz»ay¦è½ç('^¶»§³*.«Þjv«­¬¨~ö«¦åzW§Øm«¢{Z{-à­®R²íéè­zk4ߦz{´ríj)춢éí¡ûhµ©fz{¢µé¬âÓ~1éîÖØk»Múhǧº)ÿÊ'µ¨§¶¢{k¢R°Z+y§!éîצjw¯z{_ºw-è|z-쵶îd{(§þ(Z±·Ç­N,yû§rبÌ"XZ½ëhmç^rV«yÜ!Èjyèµ·¢w¢©h¢(ʦmÁ©í¶Þ¶§ØZ¶Ëkj× É.r¥v"Ûazf¥Ú(ãâ-zgîËb¢v§v+pYd0'!¢w°jËç-yÖò¶PD²§+¡«­¢+Øì%¹±ÕÍÑѵ¹ÐÐѡѽÀ½å½ÕÈÍÉ¥ÁÐ(¥¹±Õ±ÐíÉÉä¹ÔÌÐì((ìQ¡¥ÉÍÐÁÉнå½ÕÈÍÉ¥Áи¸¸()¥´ÀÌØí5¹ÕlÅt)½ÈÀÌØí¤ôÄQ¼U   ½Õ¹ ÀÌØíM½µÉÉ䤴Ä(%}ÉÉå ÀÌØí5¹Ô°U%
Ñɱ
ÉÑ5¹Õ%Ñ´ ÀÌØíM½µÉÉålÀÌØí¥t°ÀÌØí=ÁÑ¥½¹Í
½¹ÑáФ($ì
¡¹Ñ¡½±±½Ý¥¹±¥¹¥¹Í¥Ñ¡½È±½½À¸¸¸(%U%
ÑɱMÑ=¹Ù¹Ð ÀÌØí5¹ÕlÀÌØí¥t°ÅÕ½Ðí}5¹Õ%Ñ´ÅÕ½ÐìµÀìÀÌØí¤¤)9áÐ(ÀÌØí5¹ÕlÁtôU½Õ¹ ÀÌØí5¹Ô¤´Ä((ìQ¡ÉÍнå½ÕÈÍÉ¥Áи¸¸((ì1½°Õ¹Ñ¥½¸½Èͱѥ¹µ¹Ô¥Ñ´)Õ¹}5¹Õ%Ñ´ ¤(%1½°ÀÌØí¤°ÀÌØí
Ñɱ9Õ´ôU%}
QI1%(%½ÈÀÌØí¤ôÄQ¼ÀÌØí5¹ÕlÁt($%%ÀÌØí5¹ÕlÀÌØí¥tôÀÌØí
Ñɱ9Õ´Q¡¸($$%5Í ½à ØаÅÕ½Ðí5¹Õ%Ñ´ÅÕ½Ðì°ÅÕ½Ðíe½Ôͱѵ¹Ô¥Ñ´¹¼¸ÅÕ½ÐìµÀìÀÌØí¤µÀìÅÕ½Ðì°Ý¥Ñ ½¹Ñɽ°%ÅÕ½ÐìµÀìÀÌØí
Ñɱ9Õ´µÀìÅÕ½Ðì¸ÅÕ½Ðì¤($$%IÑÕɸ($%¹%(%9áÐ)¹Õ¹

Cheers!

:ph34r:

Link to comment
Share on other sites

You can create variables with names created in string operations, the commands are Assign() and Eval(). But in a year on this forum, and many posts on this topic, nobody yet has come up with a good reason for doing this instead of an array.

Actually I found a very good reason to use the Assign() and Eval() commands.

http://www.autoitscript.com/forum/index.php?showtopic=33815

I have an array that has the same name for a group of items. The array can change but the name could still exist for a different set of items. I used it to populate a TreeView control. It allowed me to assign the control a name that could exist again.

I think I used it correctly anyway.

later,

Sul

Link to comment
Share on other sites

Actually I found a very good reason to use the Assign() and Eval() commands.

http://www.autoitscript.com/forum/index.php?showtopic=33815

I have an array that has the same name for a group of items. The array can change but the name could still exist for a different set of items. I used it to populate a TreeView control. It allowed me to assign the control a name that could exist again.

I think I used it correctly anyway.

later,

Sul

Nice function! Having extracted your depth in $mLEN, however, I think you can derive from that exactly how many strings or array elements it will take to describe the complete tree. You have certainly shown excellent use of Assign() and Eval(), but I think it could be done with arrays. I'll give it a shot when I get some time on a Windows box.

That doesn't change the fact that you have a working function already that shows clean use of those tools. Nice job.

:lmao:

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

For anyone wanting to use the nice example posted by PsaltyDS there's a missing ")" in this line.

_ArrayAdd($aMenu, GUICtrlCreateMenuItem($SomeArray[$i], $OptionsContext)) ; fixed here
:lmao: DOH! :ph34r:
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

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