Hello Bill,

%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);

Simplified but slightly less clear, IMHO :)

push operates on arrays. The intent is to push $k on to the array "@$children{$v}".

Correct, so with a single entry example it much clearer:

my %father = ('Cain' => 'Adam');

my %children = ('Adam' => []);

push @{ $children{'Adam'} }, 'Cain';

So no %children looks like:

Adam => ['Cain']

iterate through 'Abel' and 'Seth'

Adam => ['Cain', 'Abel', 'Seth']


A good example of why to use clearer code (IE Go buy and apply "Perl Best Practices" by Damian Conway, its the best Perl book I've ever seen. It will make you write better code which is, among other things, less confusing, like the example in the book you mentioned.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to