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

How do I create an array that has a variable in it's name. For example:

$var = 1;
@sum$var = (1,2,3);

so if I wanted to access the array, it would be named @sum1

Replies are listed 'Best First'.
Re: Arrays names containing variables
by demerphq (Chancellor) on Jul 26, 2002 at 11:58 UTC
    You need to have a really good reason for doing this.
    no strict refs; @{"sum$var"}=(1,2,3);
    Much much much better is to use a hash or array. Since the indexing will be numeric an array is ideal.
    my @sums; $sums[1]=[1,2,3]; print @{$sums[1]}; push @{$sums[1]},"foo";

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

Re: Arrays names containing variables
by wertert (Sexton) on Jul 26, 2002 at 09:26 UTC
    interesting idea ! I've never tried this. I would probably go for a slightly different design to get round the problem. something like an array of array or even hash of arrays
    my(@sum); $var=1; $sum[$var]=[1,2,3]; $var=4; $sum[$var]=[5,7,8]; or ( better ); my(%sum) $var=1 $sum{$var}=[1,2,3] $var=5 $sum{$var}=[5,6,7]
    hope it helps wertert
Re: Arrays names containing variables
by Basilides (Friar) on Jul 26, 2002 at 09:09 UTC