in reply to Scope when initializing with references in a loop

I confess I don't understand the context for this question (i.e. *why* one wd do this) so I may be barking up the wrong tree, but the snag I see is that the first couple of times round the loop $array[1] is undefined.

This:
$array[0] = { val => "value number 0", one => 'undef' }; for my $i (1 .. 3) { $array[$i] = { val => rand(time)%3, one => \$array[0]{val} }; }
Produces this:
# undef # SCALAR(0x176f1c4) # value number 0 # SCALAR(0x176f1c4) # value number 0 # SCALAR(0x176f1c4) # value number 0
... which seems to be a bit more like what you wanted. But I'd love to know why you wanted it!

§ George Sherston

Replies are listed 'Best First'.
Re: Re: Scope when initializing with references in a loop
by IraTarball (Monk) on Dec 06, 2001 at 03:57 UTC
    *why* one wd do this)
    It might be hard to see with this example, but the actual code has 'nodes' defined, each with references to it's "neighbors". The reference seemed clean because I could get at the values I want without having to figure out what the indices are for all of my neighbors. For this array it may seem trivial, but imagine an array of n dimensions.

    the snag I see is that the first couple of times round the loop $array[1] is undefined.
    This doesn't seem to matter. I was carefull to post the code in a way that the "d/l code" link would give you the actual code I'm referring to. If you do, and you run it, the array values all get initialized in the outer most scope by first. Still, in the loop, I seem to get a reference to some new value.

    Ira,