John W. Krahn wrote: > Peter Fleck wrote: > > > > I swear that some day I will understand references. In the > > meantime... > > > > I wrote some perl that used a hash where each value was a > > reference to an array. No problem. Here is the segment from > > within a foreach loop that populates the hash. > > > > @storedates = ($sdates, $idates); > > $datestore{$oldgrantid} = [EMAIL PROTECTED]; > > > > I thought this would work. But it doesn't. I end up with the same > > array reference for each key and the same values, the last ones of > > the series. > > > > The fix was to change it to: > > > > $datestore{$oldgrantid} = [$sdates, $idates]; > > > > Using the same array reference name (@storedata) does not work > > even though the values are different. > > > > I'm interested in a bit more explanation about why this didn't > > work. I thought I was 'getting' it until this latest struggle. > > It depends on the scope of the variable. You do have warnings and > strict enabled don't you? > > my @array1; # file scope > > foreach ( @something ) { > > my @array2; # block scope > > # In the loop both @array1 and @array2 are visible > # The address of @array1 stays the same for each > # iteration of the loop. > # The address of @array2 is different for each > # iteration because my() creates a new variable > } > > If you are not using my() to declare the variable inside the loop > then the variable will always have the same address.
Just for the sake of persnicketiness, the address of @array2 will only be difference each time around the loop if its reference count is non-zero. If a reference to it isn't saved then it will just be garbage collected and reallocated in the same place. :) Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]