Because
$rec is declared within the loop the hash/reference storage with the array is OK.(if uncommented as you said)
Initially when I tried to prove this I saw the same memory address printed each iteration - so I thought you were onto something. I always thought declaration of a lexical within a loop meant new memory was allocated at that declaration, but apparently perl is smart enough that if you're not storing a reference away of the variable that it doesn't allocate new space for it on the next iteration.
Simply storing away a reference to the variable though changes all that though.
my @array;
for ( 0 .. 5)
{
my $foo;
$foo->{x} = $_;
print "$foo->{x} - (Foo = " . $foo . ")\n";
push @array, $foo if (defined($ARGV[0]));
}
for (my $y = 0; $y < scalar(@array); $y++)
{
print "Stored Value at $y: $array[$y]->{x}\n";
}
=pod
When run without $ARGV[0] being defined:
0 - (Foo = HASH(0x182f168))
1 - (Foo = HASH(0x182f168))
2 - (Foo = HASH(0x182f168))
3 - (Foo = HASH(0x182f168))
4 - (Foo = HASH(0x182f168))
5 - (Foo = HASH(0x182f168))
and with it defined:
0 - (Foo = HASH(0x182f168))
1 - (Foo = HASH(0x1825490))
2 - (Foo = HASH(0x18254d8))
3 - (Foo = HASH(0x1825520))
4 - (Foo = HASH(0x1825568))
5 - (Foo = HASH(0x18255b0))
Stored Value at 0: 0
Stored Value at 1: 1
Stored Value at 2: 2
Stored Value at 3: 3
Stored Value at 4: 4
Stored Value at 5: 5
=cut
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.