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

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

Replies are listed 'Best First'.
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.
Re: Variable as name of array
by fishmonger (Chaplain) 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"; }

        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.



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 toolic (Bishop) on Nov 01, 2012 at 20:36 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001866]
Approved by state-o-dis-array
[Corion]: But then, maybe that's just my experience with things, and maybe Hacker News is just people much younger who haven't seen more than one tech cycle...
[choroba]: s/water/bear/ and s/enough/too much/
[choroba]: beer
[choroba]: oh ok
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)
[ambrus]: Ok, the docs is somewhat unclear. It does say that when an object is garbage collected, it will get cleaned up, and eventually can no longer get messages. It's not clear how long this takes, eg. I think it's kept alive until its queued events are handled
[ambrus]: in the loop, and I'm not sure if that's ok for AnyEvent. Also, it's not clear if a Timer or File object you free really is garbage collected, i.e. that Prima doesn't keep some references to it, but I hope so.
[Corion]: choroba: No, I don't remember that story, but yes, it matches my experience ;))

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2016-12-09 10:21 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.