cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

hi all, my brain is fried, so I thought I'd "share the pain" :)

I have an arrayref of hashrefs. One of the hashrefs is itself an arrayref, ie:

$page_graphic->[$i]->{array} = @array;
OK so far, but now I want to derefence the array for a join, but:
join '', @$page_graphic->[$i]->{array}; # gives num elements
Every other guess provides the wrong answer.

Can someone point out what I'm doing wrong here? Is my assignment wrong in the first place?

Hmmm...

cLive ;-)

Replies are listed 'Best First'.
Re: dereferencing nightmares...
by merlyn (Sage) on Jan 08, 2002 at 05:24 UTC
    I have an arrayref of hashrefs. One of the hashrefs is itself an arrayref, ie:
    $page_graphic->[$i]->{array} = @array;
    Nope. Already off the track there. You've got an array name in a scalar context on the right. Therefore, a length.
    OK so far, but now I want to derefence the array for a join, but:
    join '', @$page_graphic->[$i]->{array}; # gives num elements
    Well, my guess is that eventually, you'll want this:
    join '', @{$page_graphic->[$i]->{array}};
    Every other guess provides the wrong answer.
    Just the odd-numbered guesses? {grin}

    -- Randal L. Schwartz, Perl hacker

Re: dereferencing nightmares...
by Zaxo (Archbishop) on Jan 08, 2002 at 05:27 UTC

    You get the number of elements because the assignment puts @array in scalar context. This is what you mean to do:

    $page_graphic->[$i]->{array} = \@array; join '', @{$page_graphic->[$i]->{array}};

    After Compline,
    Zaxo

      d'oh - that's what happens when I stare at the screen for too long...

      ta to both replies.

      cLive ;-)

        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.