Jump to content

Construct a legitimate variable from a string?


Recommended Posts

I tried to looking into this quite a while ago and didn't really get anywhere. Again, I find myself needing to do this and am taking another stab at it. I've done some searching and can't find a way to get this to work. What I need to do is be able to construct a string that matches the name of a variable and then have the script I'm writing return the value of that variable using the constructed string. Below is an example:

$Test6_Time = 86

$identifier = 6
$string = "$Test" & $identifier & "_Time"
ConsoleWrite( "This is the actual variable: " & $Test6_Time & @CRLF & "This is the constructed variable: " & Eval($string) & @CRLF )

I thought that Eval() might do the trick but I obviously don't understand quite how Eval() works as it fails silently.

Can anyone provide some help with this?

Link to comment
Share on other sites

$Test6_Time = 86

$identifier = 6
$string = "Test" & $identifier & "_Time"
ConsoleWrite( "This is the actual variable: " & $Test6_Time & @CRLF & "This is the constructed variable: " & Eval($string) & @CRLF )

Console

This is the actual variable: 86
This is the constructed variable: 86

 

Let's see if you can spot the difference.  :)  If not, click on "Reveal hidden contents".

Spoiler

When using Eval, the string that makes up the variable name should not include the "$".

 

Edited by TheXman
Link to comment
Share on other sites

2 minutes ago, Bagel said:

Oh my god.

Does that mean you figured it out?

Link to comment
Share on other sites

Did you not run my example?  Also, did you not reveal the hidden contents, which is the explanation?

Link to comment
Share on other sites

8 minutes ago, Bagel said:

I just made ANOTHER error in omitting an "$" where there should have been one.

Actually, the problem this time was the exact opposite.  You added a "$" where one should not have been.

$string = "$Test" & $identifier & "_Time"

Should have been 

$string = "Test" & $identifier & "_Time"

 

Edited by TheXman
Link to comment
Share on other sites

Yep, apparently I have some sort of subconscious issue that's crossing my wires when the "$" is involved. Not sure the forums can help me with that. Actually, I think this time it was a conceptual issue regarding variables. It's ingrained in me that variables MUST begin with a "$" and I think that led me to make an implicit assumption about what Eval() was doing. Conceptually, Eval() and Assign() etc. are a bit abstract to me. Thanks again for the help!

Edited by Bagel
Link to comment
Share on other sites

4 hours ago, Bagel said:

Yep, apparently I have some sort of subconscious issue that's crossing my wires when the "$" is involved.

Perhaps the crossed wires come from the differing requirements of the sister functions Eval and Execute, as shown here:

Eval:

Local $iA = Eval("iB")

Execute:

Local $iA = Execute("$iB + 1")

To my mind, Eval should be called Recall() (to go with Assign()), and Execute should be renamed Eval() cuz thats what it does.

Code hard, but don’t hard code...

Link to comment
Share on other sites

And both functions should only be used in extremely rare, very specific situations.

IMHO: never in regular code.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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