Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Interpolation in variable names

by ddc_nh (Initiate)
on Mar 16, 2010 at 09:57 UTC ( [id://828863]=perlquestion: print w/replies, xml ) Need Help??

ddc_nh has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I am trying to build the name of a variable using the content of another variable. How can I access the content of this new variable? I hope the question is clear, but I am giving here a small example: <\p> <code> my $fruit = "orange"; my $name_var = "size_$fruit"; # name of the new variable my $size = $($name_var); # ??? does not work <\code>

Seems something very trivial to do, but I cannot find how to do it in the PERL manual... Thank you very much for your kind help. <\p>

Sorry, I am very confused how to use this tagging stuff :( <\p>

Replies are listed 'Best First'.
Re: Interpolation in variable names
by Corion (Patriarch) on Mar 16, 2010 at 10:06 UTC

    You don't want to use a variable as a variable name. Dominus has said why much better than I can. Most likely, you want to use a hash to store the information about fruits:

    my %fruits = { orange => { name => 'Malus', size => 123 }, apple => { name => 'Citrus sinensis', size => 10 }, };

    You can then access your data as

    my $fruit = 'orange'; print $fruits{ $fruit }->{name}, "\n"; print $fruits{ $fruit }->{size};

    Also see tye's References Quick Reference for how to use references.

      Thanks, I did found a work around using a hash instead.

        That's not a workaround, it's the preferred way to implement the behaviour you were initially looking for.

        --
         David Serrano
         (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Re: Interpolation in variable names
by FunkyMonk (Chancellor) on Mar 16, 2010 at 10:52 UTC
    I am very confused how to use this tagging stuff
    You've used the wrong kind of slashes in your closing tags. You used <\code>, but you should have used </code>. If in doubt, read what it says immediately below the textbox on the preview page.

    Update: Added last sentence.

Re: Interpolation in variable names
by jethro (Monsignor) on Mar 16, 2010 at 10:16 UTC

    Nothing to add to Corions answer, you don't want to use that. But if you still insist or are asking just out of curiosity, this works:

    > perl -e '$size_orange=55; my $fruit = "orange"; my $name_var = "size +_$fruit";print $$name_var; print $size,"\n";' 55

    Note that $size_orange has to be a global variable, it won't work when there is a 'my' before $size_orange. Instead of $$name_var ${$name_var} would also have worked, note the curly braces instead of your parentheses

      Note that $size_orange has to be a global variable, it won't work when there is a 'my' before $size_orange.
      Just a slight nitpick, $size_orange has to be a package variable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found