Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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


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?

and you want to do this?
but why would you want to do that? Unless you really meant to do this?
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2015-11-25 06:38 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (671 votes), past polls