Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: concatenate scalar with array name

by Kenosis (Priest)
on Jan 15, 2014 at 05:15 UTC ( [id://1070645]=note: print w/replies, xml ) Need Help??


in reply to concatenate scalar with array name

Instead of attempting to create variables 'on the fly,' consider creating a hash of arrays (HoA) whose keys are array1 .. array5:

use strict; use warnings; my %hash; for my $i ( 1 .. 5 ) { for my $k ( 0 .. 4 ) { ${ $hash{ 'array' . $i } }[$k] = $i**$k; } } print "$_: @{ $hash{ $_ } }\n" for sort keys %hash;

Output:

array1: 1 1 1 1 1 array2: 1 2 4 8 16 array3: 1 3 9 27 81 array4: 1 4 16 64 256 array5: 1 5 25 125 625

Hope this helps!

Replies are listed 'Best First'.
Re^2: concatenate scalar with array name
by boftx (Deacon) on Jan 15, 2014 at 06:24 UTC

    Not a bad approach. I was thinking only in terms of what he asked, but that would accomplish the same thing. :)

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

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

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

    No recent polls found