Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Variable as name of array

by Hopfi (Novice)
on Nov 01, 2012 at 20:27 UTC ( #1001866=perlquestion: print w/ replies, xml ) Need Help??
Hopfi has asked for the wisdom of the Perl Monks concerning the following question:

Hi there! Was wondering if you can create arrays with the name of another variable. I dont know how to explain it better. Heres an example:
my @names = qw(A B C D); for(my $i=0; $i<4; $i++) { my @$names[$i] = ($i); }
Any idea if thats possible? Cheers

Comment on Variable as name of array
Download Code
Re: Variable as name of array
by toolic (Chancellor) on Nov 01, 2012 at 20:36 UTC
Re: Variable as name of array
by fishmonger (Pilgrim) on Nov 01, 2012 at 20:37 UTC
      Ah ok thanks! So IŽd do something like:
      my @names = qw(A B C D); my %order = (); for(my $i=0; $i<4; $i++) { $order{$names[$i]} = $names[$i]; } while ((my $key, my $value) = each(%order)){ print $key.", ".$value."\n"; }
      Right?

        Yes, you could do that but a Perl-style loop (see Foreach Loops) might be more idiomatic than a C-style one (For Loops). In fact, for and foreach are synonymous and can both be used interchangeably for either style of loop.

        $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my @names = qw{ A B C D }; > my %order; > foreach my $name ( @names ) > { > $order{ $name } = $name; > } > print Data::Dumper->Dumpxs( [ \ %order ], [ qw{ *order } ] );' %order = ( 'A' => 'A', 'D' => 'D', 'C' => 'C', 'B' => 'B' ); $

        Another way to construct the hash would be to use a map instead of the foreach.

        $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my @names = qw{ A B C D }; > my %order = map { $_ => $_ } @names; > print Data::Dumper->Dumpxs( [ \ %order ], [ qw{ *order } ] );' %order = ( 'A' => 'A', 'D' => 'D', 'C' => 'C', 'B' => 'B' ); $

        I hope this is helpful.

        Cheers,

        JohnGG

Re: Variable as name of array
by tobyink (Abbot) on Nov 01, 2012 at 21:05 UTC

    Have you tried? You need some extra braces in there, and need to drop the second my, but other than that your code works...

    my @names = qw(A B C D); for (my $i=0; $i<4; $i++) { @{$names[$i]} = $i; } use Data::Dumper; print Dumper(\@A, \@B, \@C, \@D);

    These are called symbolic references. It is forbidden by the strict parameter because it's usually a bad idea. That's not to say it's always a bad idea; strict refs is just about the only part of strict that's occasionally worth disabling (occasionally, in a small lexical scope).

    There are usually better ways of doing it. Here's one way, using a hash of arrays:

    use strict; my %ARRAYS; my @names = qw(A B C D); for (my $i=0; $i<4; $i++) { @{ $ARRAYS{$names[$i]} } = $i; } use Data::Dumper; print Dumper(@ARRAYS{@names});

    Here's another way using hard references instead of symbolic references:

    use strict; my (@A, @B, @C, @D); my @names = (\@A, \@B, \@C, \@D); for (my $i=0; $i<4; $i++) { @{$names[$i]} = $i; } use Data::Dumper; print Dumper(\@A, \@B, \@C, \@D);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Variable as name of array
by bart (Canon) on Nov 01, 2012 at 21:33 UTC
      Hastening to ++ this.   The Perl variable-names namespace is not intended to be a hash.   The best part of this series is definitely part-3.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001866]
Approved by state-o-dis-array
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-11-21 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls