Jump to content

Variable name -> String?


 Share

Recommended Posts

I know it's possible to turn a string into a variable with Assign() and Eval() (and even Execute()).

But is it possible to return a variable name as a string?

For example, something like

$var1 = 123

$result = SomeFunc( $var1 )

; $result now equals "var1", not 123

Thanks.

Link to comment
Share on other sites

  • Developers

I know it's possible to turn a string into a variable with Assign() and Eval() (and even Execute()).

But is it possible to return a variable name as a string?

For example, something like

$var1 = 123

$result = SomeFunc( $var1 )

; $result now equals "var1", not 123

Thanks.

Whats wrong with this ? :

$result = "$var1"

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Whats wrong with this ? :

$result = "$var1"

It'd be "var1" :whistle:. That was just an example, anyway.

To answer your question, why would you ever want to use Assign()? What's wrong with $result = "$var1", no need for Assign( "result", "$var1" ).

Of course you want Assign/Eval/Execute to do cool programatical stuff. Same with VarName->String. SomeFunc() will get me the name of the variable without having to know what the name is already.

Link to comment
Share on other sites

Of course you want Assign/Eval/Execute to do cool programatical stuff. Same with VarName->String. SomeFunc() will get me the name of the variable without having to know what the name is already.

No function available to return the name of a variable used randomly.

I would perhaps not go to this extreme, but an array in a collection of elements, so it could store the name and 1 or more values.

Global $var1[2], $var2[2]

$var1[0] = '$var1'
$var1[1] = 'value 1'
$var2[0] = '$var2'
$var2[1] = 'value 2'

MsgBox(0, 'Show me value of array $var1', $var1[1])
MsgBox(0, 'Show me which array used', $var1[0])
MsgBox(0, 'Show me value of array $var1', $var1[1])
MsgBox(0, 'Show me which array used', $var1[0])

Some debug code is planned to be added latter to AutoIt on the developers list. Perhaps your SomeFunc() may appear then.

:whistle:

Link to comment
Share on other sites

SomeFunc() will get me the name of the variable without having to know what the name is already.

you missed the whole point. If you write $name = SomeFunc($var1), that's the same as $name = "var1", because you have to write the variable name in the source code of your script in both cases!!! Think about that, or tell me a situation where you don't know the name of a variable in your script code. If there was such a situation, what would you then pass to SomeFunc() as parameter???

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

you missed the whole point. If you write $name = SomeFunc($var1), that's the same as $name = "var1", because you have to write the variable name in the source code of your script in both cases!!! Think about that, or tell me a situation where you don't know the name of a variable in your script code. If there was such a situation, what would you then pass to SomeFunc() as parameter???

Cheers

Kurt

It's not the same. It does produce the same result, sure, but even if you know the name of the variable now you're storing the data twice - once as $var1, once as "var1". And you have to spend time copy/pasting coding this for every var you want to do it with, rather than just one line with a function.

Anyway, I was just curious if such a function existed.

Link to comment
Share on other sites

It's not the same. It does produce the same result, sure, but even if you know the name of the variable now you're storing the data twice - once as $var1, once as "var1". And you have to spend time copy/pasting coding this for every var you want to do it with, rather than just one line with a function.

erm.. it's not the same. Can you please give me a sample where DON'T know the variable name!!

If you don't know the variable name, what are you going to provide to SomeFunc() ???? As soon

as you provide ANY variable as parameter to SomeFunc() you'll have to know the name of the variable, to write this line in your script source code: $name = SomeFunc($var1). The function would then return "var1". AND that's the same as $name = "var1". Reason: your source code will NOT change while the script is executed, so the name of $var1 is allways "var1". So, where is the point of this at all??

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

erm.. it's not the same. Can you please give me a sample where DON'T know the variable name!!

If you don't know the variable name, what are you going to provide to SomeFunc() ???? As soon

as you provide ANY variable as parameter to SomeFunc() you'll have to know the name of the variable, to write this line in your script source code: $name = SomeFunc($var1). The function would then return "var1". AND that's the same as $name = "var1". Reason: your source code will NOT change while the script is executed, so the name of $var1 is allways "var1". So, where is the point of this at all??

Cheers

Kurt

I just told you what the point is, re-read my post.

The point is that it's not the same. You're hard-coding and duplicating data, at the very minimum. Different (worse) method, but same result, which is what you're saying.

I can't think of anywhere offhand where you wouldn't be able to know the name of a variable, that was just a possibility I threw out there where it might be useful.

It wouldn't be the most useful function in the world, but it would be kinda cool.

Go cheers yourself

cdr

Edited by cdr
Link to comment
Share on other sites

  • Developers

I just told you what the point is, re-read my post.

The point is that it's not the same. You're hard-coding and duplicating data, at the very minimum. Different (worse) method, but same result, which is what you're saying.

I can't think of anywhere offhand where you wouldn't be able to know the name of a variable, that was just a possibility I threw out there where it might be useful.

Go cheers yourself

cdr

Nice .... :whistle:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I just told you what the point is, re-read my post.

The point is that it's not the same. You're hard-coding and duplicating data, at the very minimum. Different (worse) method, but same result, which is what you're saying.

I can't think of anywhere offhand where you wouldn't be able to know the name of a variable, that was just a possibility I threw out there where it might be useful.

Because there is none!!

Go cheers yourself

cdr

uhu, I guess there is somebody who cannot admit his fault :whistle::) No problem ...

BTW: Cheers, cheers, cheers, cheers to YOU cdr !!!!!!

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Because there is none!!

I wouldn't go that far.

Such a function did exist, just never in a public version of AutoIt. It requires pretty significant hacking about to implement. I don't remember off the top of my head why I implemented it but it was for a use I couldn't otherwise replicate without it so I remember changing my design for something. It may have had something to do with ByRef. For example:

Global $a
Test($a)

Func Test(ByRef $b)
    MsgBox(4096, "", NameOf($b))
EndFunc

In that example, "a" would be printed since that is the name of the variable being manipulated through the alias b.

Now, why is this usual you want to know? If I know $a is the in source code, it's rendundant, right? What if I don't know what's in the source code, what if I'm writing a library function. That's when it's useful.

Link to comment
Share on other sites

Now, why is this usual you want to know? If I know $a is the in source code, it's rendundant, right? What if I don't know what's in the source code, what if I'm writing a library function. That's when it's useful.

O.K., but why would you want to know the name of the variable?? The content is what matters ... Even in a library function I would not make ANY decision based on the name of a variable, no matter if the value was passed in or a pointer to that variable...

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

O.K., but why would you want to know the name of the variable?? The content is what matters ... Even in a library function I would not make ANY decision based on the name of a variable, no matter if the value was passed in or a pointer to that variable...

Cheers

Kurt

Imagine a library which reads a file and replaces specially formatted strings with the contents of a global variable in the program. Each variable would need to be registered with the file reading library. The name is then stored in a table. While reading the file, each special string is looked up in this global table and if it's there, Eval() is used to retrieve the value and insert it in place of the formatting string in the file.

It's syntactically cleaner to pass the variable itself than to pass the name of the variable. It's also less error-prone since the name of the variable is looked up at run-time instead of compile time.

Link to comment
Share on other sites

Imagine a library which reads a file and replaces specially formatted strings with the contents of a global variable in the program. Each variable would need to be registered with the file reading library. The name is then stored in a table. While reading the file, each special string is looked up in this global table and if it's there, Eval() is used to retrieve the value and insert it in place of the formatting string in the file.

It's syntactically cleaner to pass the variable itself than to pass the name of the variable. It's also less error-prone since the name of the variable is looked up at run-time instead of compile time.

O.K. that's a VERY special case and only necessary because AutoIT does not generally support pointers. So I guess, my statement ("There is no use for such a function") is still valid for 99,999 % of the AutoIT Users :whistle:

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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