I'm reading "The PERL Cookbook" and can't get past a particular line of
code. I'm trying to dissect it so I can understand exactly what's going
on.
%father = ( 'Cain' => 'Adam',
'Abel' => 'Adam',
'Seth' => 'Adam',
'Enoch' => 'Cain',
'Irad' => 'Enoch',
'Mehujael' => 'Irad',
'Methusael' => 'Mehujael',
'Lamech' => 'Methusael',
'Jabal' => 'Lamech',
'Jubal' => 'Lamech',
'Tubalcain' => 'Lamech',
'Enos' => 'Seth' );
while ( ($k,$v) = each %father ) {
push( @{ $children{$v} }, $k );
}
It's the push line I'm having difficulty with. Here's what I understand:
The outermost {} can be removed to simplify it to :
push( @$children{$v}, $k);
push operates on arrays.
The intent is to push $k on to the array "@$children{$v}".
The idiom "@$" refers to the array reference variable named immediately
after it, so as to dereference that variable and use it where one would
normally find an array variable.
e.g.
push(@$arrayRef, 15); # equivalent to push(@array, 15);
What I don't understand is what appears to be hash syntax - the {$v}.
Can someone tell me how to interpret the {$v} and the variable its
attached to? I usually try to understand a line of code from the inside
out, but even that technique fails me here.
Is there a hash involved in this line, and if so how?
--
Bill Gradwohl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>