Sign in to follow this  
Followers 0
AllenPapapetrou

Variable or Function Name Creation

14 posts in this topic

First time post:

I've tried searching for the answer but have trouble phrasing the search in AutoIt 3 Help, forum and google.

Here's the question:

How does one construct a variable or function name from variables?

For example:

I want to call the either the FileOpenDialog or FileSaveDialog in a user defined function, such as:

Function _MyFileDialog ($DialogType)
; $DialogType is either "Open" or "Save"
     $file = File[$DialogType]Dialog($DialogType & " file...",@TempDir,"All (*.*)"); Syntax?
   Return $file
EndFunc

Questions:

1. Is it possible to construct a variable or function name from variables?

2. If the answer to q 1 is "Yes" what is the construct syntax?

TIA,

Allen

Share this post


Link to post
Share on other sites



First time post:

I've tried searching for the answer but have trouble phrasing the search in AutoIt 3 Help, forum and google.

Here's the question:

How does one construct a variable or function name from variables?

For example:

I want to call the either the FileOpenDialog or FileSaveDialog in a user defined function, such as:

Function _MyFileDialog ($DialogType)
; $DialogType is either "Open" or "Save"
     $file = File[$DialogType]Dialog($DialogType & " file...",@TempDir,"All (*.*)"); Syntax?
   Return $file
EndFunc

Questions:

1. Is it possible to construct a variable or function name from variables?

2. If the answer to q 1 is "Yes" what is the construct syntax?

TIA,

Allen

The answer to your first question is no at least not in the way shown in your example. Autoit needs to know the name of the function before it runs the script.

I have shown one way to achieve the same result below.

Func _MyFileDialog ($DialogType)
; $DialogType is either "Open" or "Save"
    Select
        Case $DialogType = "Open"
            $file = FileOpenDialog($DialogType & " file...",@TempDir,"All (*.*)"); Syntax?
        Case $DialogType = "Save"
            $file = FileSaveDialog($DialogType & " file...",@TempDir,"All (*.*)"); Syntax?
    EndSelect
   Return $file
EndFunc

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Tx Bowmore

Your reply indicates a workaround I used before my post. But hints I just may have incorrect syntax. So let me rephrase the questions:

1. Does AutoIt 3 have the ability to get use a variable in a variable or function name?

2. If so, what is the correct syntax to do this?

Since I don't know the answer to q 1, I made up a syntax in my example. Your example only shows a workaround for the particular example I used.

php has this ability as in:

<?php
 $a = "Hello";
 $b = "World";
 $Hello = "Hi";
 echo "$a $b";    // produces "Hello World"
 echo "$Hello $b"; // produces "Hi World"
 echo "${$a} $b"; // also produces "Hi World" because ${$a} is $Hello
?>

Again, does AutoIt have the same functionality, albeit with similar or different syntax?

Thanks,

Allen

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

No, you cannot do that with built-in AutoIt functions. You can do it with UDF's using Call() though...

_MyFileDialog("Open")

Func _MyFileDialog($DialogType)
    ; $DialogType is either "Open" or "Save"
    $file = Call("_File" & $DialogType & "Dialog", $DialogType & " file...", @TempDir, "All (*.*)"); Syntax?
    MsgBox(0,"",$file)
    Return $file
    
EndFunc   ;==>_MyFileDialog

Func _FileOpenDialog($sTitle, $sDir, $sFilter)
    $file = FileOpenDialog($sTitle, $sDir, $sFilter)
    Return $file
EndFunc   ;==>_FileOpenDialog

Func _FileSaveDialog($sTitle, $sDir, $sFilter)
    $file = FileSaveDialog($sTitle, $sDir, $sFilter)
    Return $file
EndFunc   ;==>_FileSaveDialog

Take a look here too.

Edited by Nahuel

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Tx Bowmore

Your reply indicates a workaround I used before my post. But hints I just may have incorrect syntax. So let me rephrase the questions:

1. Does AutoIt 3 have the ability to get use a variable in a variable or function name?

2. If so, what is the correct syntax to do this?

Since I don't know the answer to q 1, I made up a syntax in my example. Your example only shows a workaround for the particular example I used.

php has this ability as in:

<?php
 $a = "Hello";
 $b = "World";
 $Hello = "Hi";
 echo "$a $b";    // produces "Hello World"
 echo "$Hello $b"; // produces "Hi World"
 echo "${$a} $b"; // also produces "Hi World" because ${$a} is $Hello
?>

Again, does AutoIt have the same functionality, albeit with similar or different syntax?

Thanks,

Allen

Sorry if I gave you false hope.

To clarify you can not use a variable in a variable or function name

You might like to have a look at the EVAL() function

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

...

php has this ability as in:

<?php
            $a = "Hello";
            $b = "World";
            $Hello = "Hi";
            echo "$a $b";     // produces "Hello World"
            echo "$Hello $b"; // produces "Hi World"
            echo "${$a} $b"; // also produces "Hi World" because ${$a} is $Hello
           ?>

Again, does AutoIt have the same functionality, albeit with similar or different syntax?

Thanks,

Allen

$a = "Hello"
$b = "World"
$Hello = "Hi"
ConsoleWrite($a & " " & $B)     ;produces "Hello World"
ConsoleWrite($Hello & " " & $B)    ;produces "Hi World"
ConsoleWrite(Eval("a") & " " & $B)  ;also produces "Hi World" because ${$a} is $Hello

This is how you do the equivalent in AutoIt.

- 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

Share this post


Link to post
Share on other sites

Bowmore, Nahuel, and The Kandle Man,

Thanks for your replies. Eval() and UDF Call will do some of what I would like.

Do you know if anyone has made a feature request like this?:

Request:

Feature:

Provide the ability to set variable assignments and function calls using cu variable names

Syntax:

$varA = "Hi"

${$varA} = "Hello"

results in $Hi = "Hello"

and

$FuncA = "FileOpenDialog"

$file = {$FuncA} (parms....)

results in $file = FileOpenDialog (parms...)

Benefit:

Simplifies coding in numerous multilevel and/or recursive situations among others.

Share this post


Link to post
Share on other sites

Request:

Feature:

Provide the ability to set variable assignments and function calls using cu variable names

Syntax:

$varA = "Hi"

${$varA} = "Hello"

results in $Hi = "Hello"

It would be nice if you actually bothered to read the help first...

$varA = "Hi"

Assign($varA, "Hello")

MsgBox(0, "", $Hi)

and

$FuncA = "FileOpenDialog"

$file = {$FuncA} (parms....)

results in $file = FileOpenDialog (parms...)

Benefit:

Simplifies coding in numerous multilevel and/or recursive situations among others.

Like you were already told, Call() and Execute() will do fine with functions defined by you, and probably even with some built-in ones.

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Bowmore, Nahuel, and The Kandle Man,

Thanks for your replies. Eval() and UDF Call will do some of what I would like.

Do you know if anyone has made a feature request like this?:

Request:

Yes, it has been requested and No, it ain't gonna happen. Assign() and Eval() are as close as you get, and using those marks your code as sloppy and poorly thought out.

Benefit:

Simplifies coding in numerous multilevel and/or recursive situations among others.

No it doesn't. Normal variables with local scope simplify those situations. Assign/Eval is for lazy people who don't want to think about scopes or learn to use arrays.

<_<

P.S. I'm sure that after getting over this indigestion I will regret sounding harsh and grumpy, but for now just know that every time you use Assign/Eval -- a baby seal gets clubbed for its fur.


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

Share this post


Link to post
Share on other sites

It would be nice if you actually bothered to read the help first...

$varA = "Hi"

Assign($varA, "Hello")

MsgBox(0, "", $Hi)

Like you were already told, Call() and Execute() will do fine with functions defined by you, and probably even with some built-in ones.

Siao,

I did read the manual and according to the manual you don't understand my issue wrt the level of programming sophistication I would like. I am looking for a ubitquitous solution via a language construct such as in php and other languages, not klutzy workarounds using existing language facilities.

Obviously, I was looking for a language construct in AutoIt which does not exist. Finding language facilities, like Assign(), which do not resolve my issue is only a workaround answer. The hardest thing to find is something that does not exist. Obviously, I will use the workarounds as long as the construct I want doesn't exist.

I've been using AutoIt professionally and personally since version 1, and I ran into my first platform issues around 1965 in Fortran programming of Astronomical Precession. (Try debugging Fortran code when the University's 1403 printer only has a business print train.)

I feel you assumed too much about me and that you owe me an apology.

Thanks,

Allen

P.S.: Maybe you're having a bad day. If so, I hope thing turn up for you soon!

Share this post


Link to post
Share on other sites

Yes, it has been requested and No, it ain't gonna happen. Assign() and Eval() are as close as you get, and using those marks your code as sloppy and poorly thought out.

No it doesn't. Normal variables with local scope simplify those situations. Assign/Eval is for lazy people who don't want to think about scopes or learn to use arrays.

<_<

P.S. I'm sure that after getting over this indigestion I will regret sounding harsh and grumpy, but for now just know that every time you use Assign/Eval -- a baby seal gets clubbed for its fur.

PsaltyDS and all of y'all,

Having programmed almost continuously since my first professional job in 1965 I've been where most people alive have never even dreamed. I know a lot and am always learning. Maybe y'all have met folks who think this or that facility is only used by lazy folks or their ilk. In general I disagree.

PsaltyDS, I disagree with your opinions. If you want to discuss it further let's take it off the forum. I'll give you one of my phone numbers and we can talk about it.

Very quickly, my questions were answered and I'm very appreciative.

Aloha,

Allen

Share this post


Link to post
Share on other sites

But... but... what about the baby seals...?

Can you still use Assign() and Eval() now that you know about the baby seals?!

<_<

<hint> You're supposed to be picking up on a little tongue-in-cheek humor here. <\hint>

Now, all seriousness aside: what kind of call would you really make that requires string naming your function? The example in the original post certainly doesn't call for it; it's a simple switch, and has no error checking. If something were passed that generated an invalid function call, it would crash the script. (And this is aside from the fact that you can only use Call() with your own local function, not for AutoIt built-ins like FileOpenDialog, as pointed out earlier.):

Func _MyFileDialog($DialogType)
    ; $DialogType is either "Open" or "Save"
    Switch $DialogType
        Case "Open"
            $file = FileOpenDialog("Open file...", @TempDir, "All (*.*)")
        Case "Save"
            $file = FileSaveDialog("Save file...", @TempDir, "All (*.*)")
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Return $file
EndFunc

:)


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

Share this post


Link to post
Share on other sites

But... but... what about the baby seals...?

Can you still use Assign() and Eval() now that you know about the baby seals?!

...

<hint> You're supposed to be picking up on a little tongue-in-cheek humor here. <\hint>

Now, all seriousness aside: what kind of call would you really make that requires string naming your function?

...

Nice to see your sense of humor. BTW, I like your TUX. Around 1995/1996 I think I was the first or second to install Slackware Linux at work on an IBM Token Ring network. Before I retired in 2001 I was running Linux with WinNT under it via VMware and using VNC to control Macs and other PCs on the LAN.

Variable Function calls in PHP are not restricted to UDFs as in AutoIt 3. Forgetting that restriction for the moment. The actual setup and call differs a bit in PHP, in a very, very simple form assuming a function exists named "Blot":

$func = "Blot";
$func(); // Calls function Blot() difined w/o parms

; or

$func = "Blot";
$func('Blip'); // Calls function Blot($parm) where "Blip" is the value of $parm

I'll return to Variable Function Name after this: Instead of constructs like Assign() or Eval() here are snippets from some

some PHP code I wrote for my own web site:

...
  $DateLines = count(${$HiArray[$HiArrayCount]});
  for ($k=0;$k<$DateLines;$k++){
   $SpanText = $SpanText.${$HiArray[$HiArrayCount]}[$k];
  }
...
if ($GotJournalFile){
 for ($i=0;$i<$TotalHighlights;$i++){
  echo '<hr class="Q3">';
  $DateLines = count(${$HiArray[$i]});
  for ($k=0;$k<$DateLines;$k++){
   $HighlightLine =${$HiArray[$i]}[$k];
   $HighlightLine = str_replace('name="','href="Root.php? ...  $TheYear.'#',$HighlightLine);
   echo $HighlightLine;
  }
 }
}
else echo "<h2>No journal entries thus no highlights</h2>";
...

Even I have difficulty reading this code having been away from it for a couple of years. However, at the time (and now) it made slick simple code for what I wanted to do.

I created a form driven calendar page, and a journal page, which defaulted to the curent month and year. From the form you can change the month and year to present a grid formated calendar. I extracted the <h2>...</h2> caption from the "Journalyyyymm.php" file I used to create the Journal page for that year and month. I then set the caption string to display when you hovered the mouse over the cell with a highlighted number for that day. Additionally, if the caption interested you, you could click on the highlighted day number which was a link to the spot of the entry in that journal page.

Syntax like this: $DateLines = count(${$HiArray[$i]});

and this: $HighlightLine =${$HiArray[$i]}[$k];

made building the page, both conceptually and programmatically, very parsimonious and elegant. In building apps with AutoIt I would like to have the same ease.

Now with Variable Function names I would like the same facility to construct n-dimensional arrays which contain, amongst other items: the prefixes, suffixes, and root name sections of Function names and Variable names. I don't need to have this facility, but I want to have it.

Perhaps if you think of this as having multidimensional, relational, and on-the-fly databases you may see some of what I like in a language.

PHP, with the widespread interest in the www, has received much more programming support than AutoIt. Both are outstanding in what they do. Maturity: feature-richness, sophistication, and robustness, will take longer for AutoIt with fewer programmers.

Jonathan Bennett and the AutoIt Team have done a magnificent, spectacular, job as I've watched AutoIt progress from v 1.

<_<

Aloha,

Allen

Share this post


Link to post
Share on other sites

Variable Function calls in PHP are not restricted to UDFs as in AutoIt 3. Forgetting that restriction for the moment. The actual setup and call differs a bit in PHP, in a very, very simple form assuming a function exists named "Blot":

Don't know nuth'n 'bout no PHP. Or much else, for that matter. I'm not a programmer. I went from 20 years of fixing hardware to setting up and maintaining it. That requires some scripting, not really programming, and I'm NOT a programmer. Not being a programmer, I obviously don't speak for or represent the AutoIt developers.

This however has never stopped me from having an opinion on this or anything else I don't fully understand! :)

That said, the feature you are talking about seems (by my dimmer lights) to arise by comparing a compiled language to a strictly interpreted one. There is no actual compiler anywhere in AutoIt, as I understand it, but an interpreter packed with the actual script (though expanded for #include's and byte encoded). PHP actually compiles at least to a VM - which (getting in way over my head here) translates things like functions called by a variable string into a look up table or switch/case form of some kind with memory addresses and lower level memory structures and pointers in place of the function's string name 'alias'.

Hmmm... AutoIt must do something interesting along that line to make Call() work, but I have no idea if it's done by modification of the encoded script text, or by lower-level C++ magic inside the interpreter binary... <_<

Perhaps if you think of this as having multidimensional, relational, and on-the-fly databases you may see some of what I like in a language.

I guess that's how programmers talk dirty to each other... geeks. :P

PHP, with the widespread interest in the www, has received much more programming support than AutoIt. Both are outstanding in what they do. Maturity: feature-richness, sophistication, and robustness, will take longer for AutoIt with fewer programmers.

Jonathan Bennett and the AutoIt Team have done a magnificent, spectacular, job as I've watched AutoIt progress from v 1.

Here, here!

But I hope it doesn't morph AutoIt into anything as, say (what would be the word?), Heavy as PHP. The small, tight lightness of AutoIt is what makes it useful to non-programmer dummies like me.

Thanks for the PHP examples. Got me thinking of new things.

I'll stop hijacking your thread now.

Save a baby seal, avoid Assign/Eval/Call!

:)


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

Share this post


Link to post
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
Sign in to follow this  
Followers 0