Jump to content

Variable Interpolation?


Recommended Posts

One of the features I love about PHP is the ability to do variable interpolation when using double quotes around string.

How could you do this in AutoIT? or is it not possible?

Example:

$Name = "Brian"
$Money = "1.99"
ConsoleWrite("Hello world, my name is $Name and I have \$$Money")

Would output:

Hello world, my name is Brian and I have $1.99
Link to comment
Share on other sites

Try this.

$Name = "Brian"
$Money = "1.99"

ConsoleWrite("Hello world, my name is " & $Name & " and I have $" & $Money)

ConsoleWrite(@CRLF & "or" & @CRLF)

ConsoleWrite(StringFormat("Hello world, my name is %s and I have $%.2f", $Name, $Money) & @CRLF)

I know of the current ways to print out variables in a string. The first example isn't even variable interpolation (Wikipedia)

The 2nd example isn't what I was after.

Edited by ParoXsitiC
Link to comment
Share on other sites

Look in the Help file for Opt("ExpandVarStrings", 1).

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Look in the Help file for Opt("ExpandVarStrings", 1).

Awesome, never knew.

However I wish they would of went with conventional methods like my original post. This to get the output from my original post in AutoIT:

Opt("ExpandVarStrings", 1)
$Name = "Brian"
$Money = "1.99"
ConsoleWrite("Hello world, my name is $Name$ and I have $$$Money$")
Link to comment
Share on other sites

While on this topic, does anyone have an update for SciTE that correctly formats with this on?

What formatting problem do you get?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I would expect the variables to be red.

They are red. (And "String" is String.)

... to get the output from my original post in AutoIT:

Opt("ExpandVarStrings", 1)
$Name = "Brian"
$Money = "1.99"
ConsoleWrite("Hello world, my name is $Name$ and I have $$$Money$")

... You need to write a substitute/user function and use that one instead of the internal ConsoleWrite() function.

Kinda to newbie kinda questions for you, if you ask me.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

They are red. (And "String" is String.)

... You need to write a substitute/user function and use that one instead of the internal ConsoleWrite() function.

Kinda to newbie kinda questions for you, if you ask me.

It uses variable interpolation, I would suspect the formatting to be similar to how most syntaxes recognize variables in PHP variable interpolation strings.

See this screenshot of notepad2 and PHP for example:

http://cl.ly/1f2K0v0D3P1z3L2v3K3h

Notice how the interpolated variables are still marked as normal variables, despite being in a string.

----------

Also, that consolewrite works fine? I was posting that is the -working- way.

Link to comment
Share on other sites

Awesome, never knew.

However I wish they would of went with conventional methods like my original post. This to get the output from my original post in AutoIT:

Opt("ExpandVarStrings", 1)
$Name = "Brian"
$Money = "1.99"
ConsoleWrite("Hello world, my name is $Name$ and I have $$$Money$")

Well, it works the way you were wanting it to work, except the syntax is slightly different (and with less typing in AutoIT) so I'm not sure why you'd want AutoIT to be PHP? How is PHP's method conventional, do all other languages do it the exact same way as PHP? If they don't then is it a convention, or just how you're used to doing it? Just wondering because I've only used the BASIC based languages myself to program and haven't really touched much on others.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Well, it works the way you were wanting it to work, except the syntax is slightly different (and with less typing in AutoIT) so I'm not sure why you'd want AutoIT to be PHP? How is PHP's method conventional, do all other languages do it the exact same way as PHP? If they don't then is it a convention, or just how you're used to doing it? Just wondering because I've only used the BASIC based languages myself to program and haven't really touched much on others.

I only know of PHP,PERL,Ruby who do variable interpolation. PHP & Perl have it so just declaring the variable as you would out of a string. For things like objects or arrays you would put them in brackets {}. Ruby doesn't have a variable identifier (test = "Hello world" as compared to $test = "Hello world") and therefore uses #{}.

More discussion of it here, and the "language envy": http://devlicio.us/blogs/sergio_pereira/archive/2009/01/17/language-envy-string-interpolation.aspx

Interestingly, VB, javascript, Java, C++, C# all don't have variable interpolation. I assume maybe because those that implemented it are most web languages that print/output a lot of information (PHP & PERL & Ruby).

It's noted that C# has an implementation (http://bugsquash.blogspot.com/2009/01/poor-man-string-interpolation-in-c.html)

VB has one too (http://haacked.com/archive/2009/01/04/fun-with-named-formats-string-parsing-and-edge-cases.aspx)

and Javascript: http://ajaxian.com/archives/secure-string-interpolation-in-javascript & http://stackoverflow.com/questions/1408289/best-way-to-do-variable-interpolation-in-javascript

etc...

It's noted that if a variable doesn't have an identifying symbol like AutoIT's, PERL's or PHP's dollar sign ... Such as Ruby, VB, C#, and Javascript, Java, then they would normally opt to use {} to encase the variable or some variation of brackets. However based on the implementation of the C# and one of the Javascript's they opted for the PHP style of using dollar signs, despite not even using dollar signs for variables normally.

So therefore, yes. I would say since AutoIT variables use $, that the "conventional" method would be to use $ instead of $..$ for variable interpolation.

Edited by ParoXsitiC
Link to comment
Share on other sites

I think the way that AutoIT does it makes sense just because it follows the same format as when using the ExpandEnvStrings. Because AutoIT uses the $ to identify variable names, it has to have some way of making sure it doesn't expand the variables you don't WANT expanded. If you wanted to put the name of a variable in a string to be printed to the console or a message box, if you just used the variable name in the string, it would expand every occurence of the variable in a string which isn't what's wanted.

The only deviation from the "convention" is that in AutoIT you put the $ after the variable name instead of in front of it, you're still using the $ to identify that you want the variable to be interpolated, you're just putting it in a different location. Different languages do many things differently than other languages, it's part of the learning curve.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I think the way that AutoIT does it makes sense just because it follows the same format as when using the ExpandEnvStrings. Because AutoIT uses the $ to identify variable names, it has to have some way of making sure it doesn't expand the variables you don't WANT expanded. If you wanted to put the name of a variable in a string to be printed to the console or a message box, if you just used the variable name in the string, it would expand every occurence of the variable in a string which isn't what's wanted.

The only deviation from the "convention" is that in AutoIT you put the $ after the variable name instead of in front of it, you're still using the $ to identify that you want the variable to be interpolated, you're just putting it in a different location. Different languages do many things differently than other languages, it's part of the learning curve.

I am sure the devs had a reason for it. BTW, with PHP if you don't want to expand it you use \$var instead of $var. Infact thats how you would print dollar signs..just escaping them.

Back to the topic at hand, I doubt SciTE could really format for it, since its a optional opt() that you set within AutoIT. SciTE would need to follow the logic of the program just to get an idea when to expand and when not to expand. Example:

$Name = "Brian"

Expand()

ConsoleWrite("Hello world, my name is $Name$")

UnExpand()

ConsoleWrite("Hello world, my name is $Name$")

Expand()

ConsoleWrite("Hello world, my name is $Name$")

Func Expand

    Opt("ExpandVarStrings", 1)

EndFunc

Func UnExpand

    Opt("ExpandVarStrings", 0)

EndFunc

I assume SciTE uses a type of regular expressions to figured out how to format things. That would be impossible to format correctly...

Edited by ParoXsitiC
Link to comment
Share on other sites

Think you placed your topic in the wrong forum.

... *Grabs bag of popcorn* ...

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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