Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: references--hard vs anonymous operational weirdness

by bobf (Monsignor)
on Mar 23, 2008 at 03:40 UTC ( [id://675724]=note: print w/replies, xml ) Need Help??


in reply to references--hard vs anonymous operational weirdness

As mentioned by both hipowls and Fletch, the issue is likely that your @array is declared outside of the loop. If you wanted to prove to yourself that a new array ref is being created, or that an existing one is being reused, you can examine the memory address by printing the reference (see perldsc). The following example illustrates the difference between an array declared outside the loop and one that is declared on every iteration.

use strict; use warnings; my %hash; my $same_array = []; for( 1 .. 3 ) { $hash{$_}{same} = $same_array; $hash{$_}{new} = []; print "$_: $hash{$_}{same} vs $hash{$_}{new}\n"; }
Output:
1: ARRAY(0x34f88) vs ARRAY(0x35d50) 2: ARRAY(0x34f88) vs ARRAY(0x1854e88) 3: ARRAY(0x34f88) vs ARRAY(0x1854e34)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://675724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found