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

Fellow Monks!
I need your help!
In a nutshell I want to sort 12 months of single value data into 12 corresponding arrays. Sure I could do 12 if-then statements, but I would rather do it in a more sleak fashion such as what I would call a variable array naming scheme. The 12 arrays would already be predefined @stat1....@stat12. However, when it comes time to sort the data into its appropriate monthly array I would think that I would need to do something like this @statN, where N is the month number of the array that the value is supposed to go into, i.e. month 2 data would go into the array @stat2. Here is a little bit of non-functional code that I have attempted (by looking around for ideas) :
#!/usr/bin/perl -w use strict; my $month; my $value; $month=2; $value=300; my (@stat1,@stat2,@stat3,@stat4,@stat5,@stat6,@stat7,@stat8,@stat9,@st +at10,@stat11,@stat12); my $arrayname = 'stat'.$month; push @$arrayname, $value; my $nr=@stat2; print "NR $nr\n"; print "$stat2[0]\n";
This is an attempted test of prelim. concepts or so I thought. Could you please advise me as to where or how I am going wrong.
Any help that you can provide will be greatly appreciated.
Thank You In Advance!!

Replies are listed 'Best First'.
Re: Constructing Variable Arrayname
by Corion (Patriarch) on Dec 20, 2004 at 19:02 UTC

    Dominus has said it much better than I can, in his series about using a variable to construct the name of a second variable. You almost never want to do that.

    In your case, you are most likely better off in constructing an Array of Arrays ("AoA"):

    # verbose: my @stats; # Initialize the 12 months: my $months = 12; for (0..$months-1) { $stats[$_] = []; # empty array at first }; my $month = 2; my $value = 300; push @{ $stats[ $month ] }, $value; my $nr = 2; print join ",", @{ $stats[ $nr ] };

    You might also want to consider using a database like SQLite for your statistics, as you can then more easily aggregate not only by month but also by other criteria. In any case, go and read Dominus' article, and after that, tyes References quick reference.

    Update:As I just noticed, my months go from 0 to 11, so $month = 2 means March now and not February. That's something you should be aware of...

Re: Constructing Variable Arrayname
by Fletch (Bishop) on Dec 20, 2004 at 19:01 UTC

    Yup, you're heading down the wrong path (using symbolic references). You really want a nested array of arrays. See perldoc perlreftut and perldoc perllol, then check out perldoc perldsc and perldoc perlref.

Re: Constructing Variable Arrayname
by Eimi Metamorphoumai (Deacon) on Dec 20, 2004 at 19:03 UTC
    What you want is an array of arrayrefs.
    #!/usr/bin/perl -w use strict; my $month; my $value; $month=2; $value=300; my @stat; push @{$stat[$month]}, $value; my $nr=@{$stat[2]}; print "NR $nr\n"; print "$stat[2]->[0]\n";
Re: Constructing Variable Arrayname
by dave_the_m (Monsignor) on Dec 20, 2004 at 19:08 UTC
    I think you want a single array-of-arrays variable rather than 12 individual variables, eg
    push @{$stats[$month]}, $value; ... for my $month (1..12) { print "NR ", scalar(@{$stats[$month]}), "\n"; print $stats[$month][0], "\n"; }

    update: As Corion reminded me, that should really be 0..11

    Dave.