in reply to Re: dereferencing nightmares...
in thread dereferencing nightmares...

You may know this already but... if you end up doing this as Zaxo suggested, then your @array better be my()ed in the loop for $i; otherwise, all elemets of $page_graphic->[$i] will point to the same set of values (probably not what you want).

You could do
$page_graphic->[$i]->{array} = [@array]; # ... other stuff ...
or
loop_construct { my @array = @other_array; $page_graphic->[$i]->{array} = [@array]; # ... other stuff ... }
Regards.

Replies are listed 'Best First'.
Re: Re: Re: dereferencing nightmares...
by Anonymous Monk on Jan 08, 2002 at 20:39 UTC
    err... obviously i ment
    loop_construct { my @array = @other_array; $page_graphic->[$i]->{array} = \@array; # ... other stuff ... }
    Sorry about that.
Re(3): dereferencing nightmares...
by dmmiller2k (Chaplain) on Jan 09, 2002 at 00:15 UTC

    Perhaps this is beating a dead horse, but it appears you've hit upon what cLive ;-) apparently originally meant to do with this.

    $page_graphic->[$i]->{array} = [@array];

    By specifying the square brackets around @array, assigning a reference to an anonymous array containing copies of the elements of @array.

    No need of correction (IMHO), since cLive ;-) didn't give us the full context of his code (i.e., how is @array populated?), although declaring @array with my within the loop and using \ to take a reference to it is a perfectly valid Way To Do It.

    Generally, though, taking references to named array and/or hash variables within a loop raises a red flag with me.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
(cLive ;-) Re: dereferencing nightmares...
by cLive ;-) (Prior) on Jan 09, 2002 at 01:38 UTC
    Y'know, that is a very good point.

    It's probably only because every var I used is as localised as possible with 'my' that this has never been a problem - yes, in this has the array is purely local to the sub it's used in, but the $page reference isn't. Most of the other elements referenced are anonymous.

    I think I was definitely having too long a session yesterday. What I've done works, but I think I'll rewrite it later when i have a better idea of the general context it will be used in. And, I'll mark the reference of @ array possible issue in the comments in case it manages to creep in (unlikely, but safer :)

    Thanks again for an interesting point.

    cLive ;-)