Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Creating Arrays Dynamically

by listanand (Sexton)
on May 23, 2010 at 22:48 UTC ( [id://841292]=perlquestion: print w/replies, xml ) Need Help??

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

Hi perl monks,

I have a situation where I need to create array dynamically (on the fly). I have specific names also by which the arrays need to be created. Here's an example code snippet:

my @array_names = qw (a b c d e f);

Now, I try the follwing:

for my $name (@array_names) {

@$name=();

}

So far so good. But then when I try to access one of the arrays, say @a, the compiler complains.

Any suggestions? Thanks in advance.

Andy

Replies are listed 'Best First'.
Re: Creating Arrays Dynamically
by GrandFather (Saint) on May 23, 2010 at 23:27 UTC

    Show us the code that complains. But more importantly, tell us why you want to do this obnoxious thing (using symbolic references - see http://perl.plover.com/varvarname.html for why that's bad). Most likely there is a much cleaner fix using a hash or hash of arrays or some such.

    True laziness is hard work
Re: Creating Arrays Dynamically
by liverpole (Monsignor) on May 24, 2010 at 00:54 UTC
    Hi listanand,

    Here's one way you could do it with a reference to a hash of array refs:

    use strict; use warnings; use Data::Dumper; my @array_names = qw (a b c d e f); my $h_arrays = { }; for my $name (@array_names) { $h_arrays->{$name} = [ $name ]; } printf "I've now got these arrays in \$h_arrays: %s\n", Dumper($h_arr +ays); __END__ Output: I've now got these arrays in $h_arrays: $VAR1 = { 'e' => [ 'e' ], 'c' => [ 'c' ], 'a' => [ 'a' ], 'b' => [ 'b' ], 'd' => [ 'd' ], 'f' => [ 'f' ] };
    I'm fond of hash references, so I'd do it that way.  More simply, though, you could use a hash (instead of hash references), containing the array references:
    use strict; use warnings; use Data::Dumper; my @array_names = qw (a b c d e f); my %arrays = ( ); for my $name (@array_names) { $arrays{$name} = [ $name ]; } printf "I've now got these arrays: %s\n", Dumper(\%arrays);

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Creating Arrays Dynamically
by toolic (Bishop) on May 23, 2010 at 23:48 UTC
Re: Creating Arrays Dynamically
by choroba (Cardinal) on May 23, 2010 at 22:57 UTC
    What do you mean by the compiler complains? It works for me just fine. However, hash of arrayrefs would be a cleaner solution and would allow use strict.
      Thanks everyone for writing.

      Using a hash of arrayrefs certainly seems like a cleaner solution. I will try that.

      Andy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found