in reply to Re: Re: $_ and nested while loops w/angle operator
in thread $_ and nested while loops w/angle operator

jdporter,

Update: I should've keep reading in perlreftut! The answer is here for anyone else that is still learning how to use references. Thank you all for your help.


Thanks for the further elaboration. For some reason, I am having a hard time understanding what is happening here:

push @{$data{$query}}, $_;

From perlreftut:
"If $aref contains a reference to an array, then you can put {$aref} anywhere you would normally put the name of an array. For example, @{$aref} instead of @array."

However, in your/my code, $query is a list, %data is a hash... so why the @{} notation? What's going on there that I'm missing?

My second question is: all of the code I posted is happening in a subroutine... how do I return a reference to the HoA? Is it:

return \@{$data};

Slightly boggled,

AH

----------
Using perl 5.6.1 unless otherwise noted. Apache 1.3.27 unless otherwise noted. Redhat 7.1 unless otherwise noted.

Replies are listed 'Best First'.
Re: Re: Re: Re: $_ and nested while loops w/angle operator
by jdporter (Paladin) on Apr 08, 2004 at 19:50 UTC

    The fundamental point is that a reference is just another kind of scalar value. Anywhere you can put 3 or "ouch", you can put a reference. So an arrayref need not be the simple scalar variable in the example ($aref), it could be an element of an array ($refs[9]) or the value of a hash element ($hits{"blue"}).

    So, given that $data{$query} contains an array ref, you can push things on that array via

    push @{ $data{$query} }, $thing;

    To return a hash by reference, you treat the hash variable as an opaque container. Like so:

    return \%data;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.