xenchu has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to srudy Perl with the aid of a book titled Professional Perl Programming by Peter Wainwright et al. I decided to type in and run a hash example. The code looked like this:
p.135-136(Professional Perl Programming)
#!/usr/bin/perl # indexhash.pl use warnings; use strict; #create a hash with integrated index my %hash = { Mouse => {Index => 0, Value => 'Jerry'}, Cat => {Index => 1, Value => 'Tom'}, Dog => {Index => 2, Value => 'Spike'} }; # sort a hash by integrated index foreach (sort {$hash{$a} {'Index'} cmp $hash{$b} {'Index'}} keys %hash +) { print "$hash{$_} {'Value'} <= $_ \n"; }
Perl told me the syntax was ok but when I ran it I got the following error messages:
Reference found where even-sized list expected at indexhash.pl line 7. Use of uninitialized value in concatenatization (.) or string at index +hash.pl line 16.
My question is twofold. First, assuming I can read and type what is the error? Second, does any Perl Monk have an opinion on this book and is it worth using?
This is my first post. I hope this question is suitable.
Thanks in advance,
xenchu

Replies are listed 'Best First'.
Re: Book Code
by liz (Monsignor) on Nov 15, 2003 at 19:57 UTC
    I don't know whether this is a typo on your part, or a misprint in the book, but In think you will need to put parentheses instead of curly braces here:
    my %hash = ( # note parenthesis open here Mouse => {Index => 0, Value => 'Jerry'}, Cat => {Index => 1, Value => 'Tom'}, Dog => {Index => 2, Value => 'Spike'} ); # note parenthesis close here
    The output then becomes:
    HASH(0xfd578) {'Value'} <= Mouse HASH(0xfd494) {'Value'} <= Cat HASH(0x10e080) {'Value'} <= Dog

    Liz

Re: Book Code
by pg (Canon) on Nov 15, 2003 at 20:04 UTC

    It is either a typo on your side, or a printing error in the book. The correct syntax shall be: (it is not {}, but (), as you are defining a hash, not a hash ref)

    my %hash = ( Mouse => {Index => 0, Value => 'Jerry'}, Cat => {Index => 1, Value => 'Tom'}, Dog => {Index => 2, Value => 'Spike'} );

    If you define a hash ref, not a hash, then you {}, instead of (), the syntax shall be:

    my $hash = { Mouse => {Index => 0, Value => 'Jerry'}, Cat => {Index => 1, Value => 'Tom'}, Dog => {Index => 2, Value => 'Spike'} };

    But this does not agree with some of the later lines in your script, for example:

    foreach (sort {$hash{$a} {'Index'} cmp $hash{$b} {'Index'}} keys %hash +)

    Obviously this line expects a hash, not a hash ref.

    To retrieve elements from hash ref, you say:

    $hash->{$a}
Re: Book Code
by xenchu (Friar) on Nov 15, 2003 at 21:28 UTC
    It seems I can't read.

    Thank you all very much for the help. I am getting new glasses. I appreciate both the effort and the prompt response.

    xenchu

      Welcome to the monastery! To answer your question about the Wrox book you're learning from: I've looked through it a few times and wasn't especially impressed. You'll save yourself a lot of headaches if you invest in the Camel book instead, or if that seems a bit heavy going, try the Llama book first. The investment will repay itself many times over, and you'll have a lot more fun with Perl into the bargain ;-)

Re: Book Code
by Anonymous Monk on Nov 15, 2003 at 19:58 UTC