Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

variable through another variable

by jnmfl (Initiate)
on Jan 26, 2002 at 15:08 UTC ( #141761=perlquestion: print w/replies, xml ) Need Help??

jnmfl has asked for the wisdom of the Perl Monks concerning the following question: (data structures)

I have the name of a variable stored in $var. How can I make it so $data grabs the variable name? I tried this:
$data = $$var;
I put $$var with two $'s because one '$' for the variable, then right after that the '$var' places the variables name, I hope there's a way to do this, I'm just using bad syntax and I don't know what the procedure is called.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: variable through another variable
by Juerd (Abbot) on Jan 26, 2002 at 17:01 UTC
    That will work, but not under strict 'refs'. Symbolic references are considered bad practice (but you can't always avoid them).

    $testing = "Hello, World!\n"; $var = "testing"; $data = $$var; print $data; # prints Hello, World!

    Using a hash is better, and hashes are useable under strict 'refs':
    $hash{"testing"} = "Hello, World!\n"; $var = "testing"; $data = $hash{$var}; print $data;
    (You can remove quotes from hash keys that have no \W characters: $hash{testing}.)
Re: variable through another variable
by Roy Johnson (Monsignor) on Apr 14, 2004 at 17:33 UTC
Re: variable through another variable
by metadoktor (Hermit) on Jan 26, 2002 at 17:03 UTC
    So you've done something like this?
    $var="\$somevariable";
    and you want to do this?
    $data=$var
    now...
    $data="$somevariable"
    but why would you want to do that? Unless you really meant to do this?
    $data=$somevariable;
Re: variable through another variable
by Roy Johnson (Monsignor) on Apr 22, 2005 at 17:53 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://141761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-01-23 21:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?