Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

computed variable names

by Anonymous Monk
on Jan 13, 2000 at 08:42 UTC ( [id://2079]=perlquestion: print w/replies, xml ) Need Help??

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

I was not satisfied with "soft" references ability because it requires  no strict "refs"; and additional variable to store the name. I thought it's implemented more flexible, like in GNU make. Here's an example:
my (@arr_type1, @arr_type2, ... ); $type = get_type(); print "1st member of $type = ${arr_${type}}[0]\n";
That didn't work. Can I solve this another way?

Replies are listed 'Best First'.
Re: computed variable names
by japhy (Canon) on Jan 14, 2000 at 02:26 UTC
    If you want to use soft references, the you'll need to be a little more careful. First, you need say no strict 'refs'; if you didn't say use strict; beforehand. To do what you'd like to do, you need to use quotes like so: print "1st member of $type = ", ${"arr_$type"}[0]; However, REAL references are the way to go. Make a hash, whose keys are the possible return values of get_type(). Each value in this hash will be an array reference.
    my %arrays = ( type1 => [ "look", "at", "me", "go", ], type2 => [ "watch", "me", "exeunt", ], type3 => [ "see", "me", "disappear", ], ); $type = get_type(); print "element 1 if $type is $arrays{$type}[0]\n";
    To learn how to use references, look at the perlref documentation.
RE: computed variable names
by nate (Monk) on Jan 13, 2000 at 21:29 UTC
    As far as I know, there isn't a way to use soft references without "no strict 'refs';".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found