Let me say this yet again: What you're asking about is how to create symbolic references; ie, how to manipulate variables' names programatically (variable variable names). Perl allows you to do this. But it is broadly regarded as the wrong way to accomplish what you actually need to do. You're better off using proper references, specifically, a list of lists. You can read about how to do that in perlref, perlreftut, perllol, and perldsc.

Many newcomers think that they've come up with one of those situations where symbolic references are a good idea. They're almost universally wrong. Computer Science has developed for a half of a century in a direction that separates a program's data from the program's source itself. Variable variable names (symbolic references) muddle that distinction, possibly with unhappy results. While there are a few useful uses of symbolic references, symbol table manipulation is best left to a few experts. Just as you don't need to know how to build a microprocessor in order to use a microprocessor, you don't need to worry about manipulating the symbol table in order to benefit from its existence. To be frank, you haven't come up with a new good reason to use symbolic references. It's been addressed, rehashed, and meditated upon. The problem's been solved.

So when you feel the temptation to write, "@array1" and "@array2", or @{$array . $i}, you should instead be doing something like this:

$array->[1][4] # for a multidimensional array

Symbolic references are illegal under "use strict;", for good reason. They are error prone, and the errors they foster can be very difficult to pinpoint, and in fact, not always immediately apparent. You may not discover an error caused by misuse of symbolic references until code has been in place for a long time. Then one day you'll stumble across that special case that awakens the demon. Other times, the bugs can crop up immediately, but be extremely difficult to figure out where the offending code is.

Proper references (hard references) are easier to manage, and in most regards, more powerful. They are the basis for complex datastructures in Perl, and usually the facilitators of objects in Perl as well. They're very flexible, and yet, they don't blur the distinction between code and data, and they are unlikely to produce the kind of hard to find bugs that soft refrences can hide.


Dave


In reply to Re: adding number to array name by davido
in thread adding number to array name 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.