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...


In reply to Re: Constructing Variable Arrayname by Corion
in thread Constructing Variable Arrayname by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.