Hi Perl Monks,

I am fairly new to Perl and have recently produced the following piece of code. I have tested it extensively and it returns the desired results. However I just wondered if someone with more experience of Perl could advice me on its usability.

Basically, I have declared an array variable with the 'my' keyword inside a for loop. I then use the address of this declared array variable in another array variable that has been declared before the aforementioned for loop. The basic code structure looks like this:

my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; Code in here to populate @Array_of_Vals } push @Array_of_ALL_Vals,\@Array_Vals;

The @Array_Vals variable is, in general, a different size each time. In addition the array @Array_Vals defined inside the loop is not used anywhere else in the code and is local to the for loop.

My concern is with the declaration of the array within the for loop. Even though the name of the variable is the same each time I assume for each loop it gets assigned to a different memory address thus the values in the @Array_of_ALL_Vals array do not get overwritten?

I thought it would be better to add an incrementer to the array name inside the for loop so the name would be different each time (@Array_Vals1,@Array_Vals2,...). However after doing some searches I found that would not be a good idea (variable name containing variable)

Many thanks for any comments

Gauss76


In reply to declare my variable in loop by gauss76

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.