Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

variable through another variable

( #141761=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie jnmfl on Jan 26, 2002 at 15:08 UTC
Q&A  > Data Structures


Description:

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.

Answer: variable through another variable
contributed by Juerd

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}.)
Answer: variable through another variable
contributed by Roy Johnson

Apparently this FAQ is not easy to find, since the question gets asked regularly.

Re: Create an array reference from a string offers helpful input from Corion and dragonchild.

Answer: variable through another variable
contributed by metadoktor

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;
Answer: variable through another variable
contributed by Roy Johnson

See Re: variable with $$.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (9)
    As of 2014-08-28 11:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (259 votes), past polls