Tuesday, August 5, 2008

AU3 - Dynamic Variable Example

Here's an example of how to create a dynamic variable in AU3.
$Fu    = "World!"
$Bar = "Fu"
$Fubar = "Hello " & Execute("$"&$Bar)
In this example, $Fubar is set to "Hello World!"

Here's a function that will convert all variables and macros in a string into their stored values.
Func ParseMacros($Str)
Global
Local $Macros = StringRegExp($Str,"([\$\@]\w+)",3)
If Not @error Then
For $i=0 to UBound($Macros) - 1 step 1
$Str = StringRegExpReplace($Str,"\"&$Macros[$i],StringReplace(Execute($Macros[$i]),"\","\\"))
Next
EndIf
Return $Str
EndFunc
For example, using the variables from the first code, this will evaluate to "Hello World!"
ParseMacros("Hello $Fu")
I find this useful for scripts where you need to store dynamic strings in a text file, because you can just use something like ParseMacros(FileRead("File.txt")) and you get a nice dynamic string to play with.

No comments: