Sign in to follow this  
Followers 0
theguy0000

setting variables with string names

14 posts in this topic

#1 ·  Posted (edited)

Well, I want to be able to set variables depending on a string representing their name...

What Iwant to is is for the user to input a variable name such as "hi", then the variable $hi will be created and set to a specific value (lets say "hello" in this case).

How would I go about doing this?

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Share this post


Link to post
Share on other sites



Maybe something like this?

$a_input = InputBox("", "", "Hi")
If $a_input <> "" Then
    Assign ( $a_input, "Hello!")
    MsgBox(0, "", Eval($a_input))
EndIf

Share this post


Link to post
Share on other sites

I never knew about assign :o thanks! (but why the Eval () ?)


The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well, I want to be able to set variables depending on a string representing their name...

I think you want this:

dim $Hi
$a_input = InputBox("", "", "Hi")
If $a_input <> "" Then
    assign(eval("a_input"),"hello")
      MsgBox(0, "", $Hi)
EndIf
Edited by jefhal

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I never knew about assign :o thanks! (but why the Eval () ?)

Eval() returns the value of a variable.

Example:

$foo = 12
MsgBox(0, 'Test', Eval('foo'))
Edited by freanir

freanir

Share this post


Link to post
Share on other sites

hmm...what on earth is the point of eval? Why use Eval ('foo') when you can just use $foo?


The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Share this post


Link to post
Share on other sites

hmm...what on earth is the point of eval? Why use Eval ('foo') when you can just use $foo?

You can find an example of this being done here in my first UDF (with help from Sven).

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

hmm...what on earth is the point of eval? Why use Eval ('foo') when you can just use $foo?

Why use
Assign('foo', 'Test')
, when you can use
$foo = 'Test'
? :o

freanir

Share this post


Link to post
Share on other sites

Why use

Assign('foo', 'Test')
, when you can use
$foo = 'Test'
? :o
Try these examples and substitute your idea with what's there. You will see the subtle difference. You are not assigning $foo = "test", you are assigning the contents of $foo to a new variable of that name to be equal to "test". Look at the last statement in my code snippet. I show $Hi, not $a_input. Big difference, but requires a specific need to be of use. We thought your question reflected that special need...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

I think you want this:

dim $Hi
$a_input = InputBox("", "", "Hi")
If $a_input <> "" Then
    assign(eval("a_input"),"hello")
      MsgBox(0, "", $Hi)
EndIf
This would not work if you didn't know what the user was going to enter. My example will work even if the user enter something other than "Hi".

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@jefhal:

:o I don't get your point.

I know what assign() and eval() are for.

My previous post was an answer to a post of theguy0000, who wrote:

Why use Eval ('foo') when you can just use $foo?

To answer his question I changed it to a question of which he knows the answer. Edited by freanir

freanir

Share this post


Link to post
Share on other sites

I was answering theguy also. Sorry for the confusion... :o


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

This would not work if you didn't know what the user was going to enter. My example will work even if the user enter something other than "Hi".

Good point big_daddy, but what would be the point of going to all that extra work of using assign and eval if he won't know the names of the variables in either case? I guess we'd have to ask theguy. It seems like it would be just as easy to assign variable names $1 $2 $3 .... as people enter their unique values.

I know what you mean, though, as I had to declare $Hi before I used it! Hard thing to do if you don't know what it's going to be!

:o:geek:


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Why use

Assign('foo', 'Test')
, when you can use
$foo = 'Test'
? :o
oh lol. I get it, thanks.

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

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