gauss76 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: declare my variable in loop
by hippo (Archbishop) on Aug 08, 2017 at 08:22 UTC | |
by gauss76 (Scribe) on Aug 08, 2017 at 08:33 UTC | |
by Corion (Patriarch) on Aug 08, 2017 at 08:35 UTC | |
by gauss76 (Scribe) on Aug 08, 2017 at 08:47 UTC | |
by Corion (Patriarch) on Aug 08, 2017 at 09:50 UTC | |
by AnomalousMonk (Archbishop) on Aug 08, 2017 at 10:01 UTC | |
by AnomalousMonk (Archbishop) on Aug 08, 2017 at 10:14 UTC | |
|
Re: declare my variable in loop
by thanos1983 (Parson) on Aug 08, 2017 at 09:51 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |