Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Create an array reference from a string

by Corion (Pope)
on Apr 12, 2004 at 19:28 UTC ( #344493=note: print w/replies, xml ) Need Help??


in reply to Create an array reference from a string

While it is possible to do what you say that you want in Perl, it is not advisable. Please read Mark Jason Dominus' excellent Why it's stupid to `use a variable as a variable name' about it. Most likely, you do want real references or a hash instead.

  • Comment on Re: Create an array reference from a string

Replies are listed 'Best First'.
Re: Re: Create an array reference from a string
by xorl (Deacon) on Apr 12, 2004 at 19:37 UTC
    So I guess you're suggesting something like:
    my @bar = qw(One Two Three); my $foo = "bar"; my %quux = ("bar", \@bar); print "***" . $foo . "***"; foreach my $baz (@{$quux{$foo}}) { print $baz . "\n\n"; }
    It looks like that will work, but it does mean I have to create a hash.
      Think about it this way - you are asking to access some data structure (scalar, array, hash, whatever) using an arbitrary string as its name. That is the very definition of a hash.

      In fact, soft references are implemented under the hood as a hash. So, it's not like you're saving anything. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-04-07 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (42 votes). Check out past polls.

    Notices?