If the arrays have significant differences (they do not have the same meaning, and won't be used the same way), then it's probably better to give them significant names (like "potatoes", "tomatoes", "toes"), in which case a hash can be used (and if the arrays aren't going to be used in the same place, there's no need for having them ordered). Then you can have $hash{potatoes}[0] or $hash{toes}[-1] for exemple.

If the arrays are equivalent sets, in which case the order may be important, then an array of arrays is probably a clearer solution (and faster, but it's not a significant change if there's little data). This means that instead of $array1[0], $array2[3], $array3[9] you would write $array[1][0], $array[2][3] and $array[3][9]. The difference is that you'll have something at index 0 (it can be undef, in which case your first array will be at 1).

Since you seem to be in the second case, here is how to do it:

for my $i (0..4) { for my $j (0..4) { $array[$i][$j] = something($i,$j); } }
Of course what I said still stands, instead of @array, $i, $j and &something, you should use meaningful names.

More info in perldsc


In reply to Re: concatenate scalar with array name by Eily
in thread concatenate scalar with array name by iThunder

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.