Re: Hash problem



Johnson, Reginald (GTI) wrote:
I am doing an example from Perl Objects, References & modules. I suspect
many of you already use this book as a reference.
My hash is showing the address instead of the name and I'm not sure
why. Here is my output.

this is person=>HASH(0x20040014)
this is who=>HASH(0x20040014)
HASH(0x20040014) is missing preserver
HASH(0x20040014) is missing sunscreen
HASH(0x20040014) is missing water_bottle
HASH(0x20040014) is missing jacket

Here is the code
#!/usr/bin/perl

use warnings;

use strict;

my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @skipper = qw ( blue_shirt hat preserver sunscreen);
my @professor = qw(sunscreen water_bottle slide_rule batteries
radio);

my %all = {
"Gilligan" => \@gilligan,
"Skipper" => \@skipper,
"Professor" => \@professor,
};

If you had included the warnings pragma then perl would have told you what is
wrong:

Reference found where even-sized list expected at Hash_problem.pl line 8.

You need to assign a list to %all, not an anonymous hash:

my %all = (
Gilligan => \@gilligan,
Skipper => \@skipper,
Professor => \@professor,
);




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.



Relevant Pages

  • Re: taking references to functions
    ... > I'm building a diagnostic medical questionnaire using Perl Tk. ... > to create a reference to a named function but not for unnamed ones. ... Notice that %dispatch here is an actual hash variable. ...
    (perl.beginners)
  • Re: Complex Objects in Perl
    ... > Objects in Perl are implemented as a reference to an ... > anonymous hash. ... > array or a hash or it may be another object (reference to ... > installation of perl: ...
    (comp.lang.perl)
  • My own handy Pocket Reference notes (please share your own also)
    ... hints/reminders from books and other usenet posts. ... Do you have some handy notes scratched inside YOUR Perl book? ... Here are my personal quick reference notes ... To pass a only single hash to a subroutine: ...
    (comp.lang.perl.misc)
  • Re: Complex Objects in Perl
    ... >> Objects in Perl are implemented as a reference to an anonymous hash. ... >> enumerate over array and hash members as you would enumerate over any ...
    (comp.lang.perl)
  • Re: Complex Objects in Perl
    ... > sub-object is made up of a number of different objects and an array of an ... Objects in Perl are implemented as a reference to an anonymous hash. ...
    (comp.lang.perl)