RE: Hash problem





-----Original Message-----
From: D. Bolliger [mailto:info@xxxxxxxxxxxx]
Sent: Thursday, September 28, 2006 4:32 PM
To: beginners@xxxxxxxx
Subject: Re: Hash problem


Johnson, Reginald (GTI) am Donnerstag, 28. September 2006 21:58:
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 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 = {

usage of '()' instead of '{}' would probably help :-)

The hash as defined in your line has one key with a stringified address,
and
the value is undef.

Check this out with

use Data::Dumper;
warn Data::Dumper::Dumper (\%all);


"Gilligan" => \@gilligan,
"Skipper" => \@skipper,
"Professor" => \@professor,
};

check_items_for_all(\%all);

sub check_items_for_all{
my $all = shift;
for my $person(sort keys %$all) {
print "this is person=>$person\n";
check_items_required($person,
$all->{$person});
} #end for
} #end check_items_for_all

sub check_items_required {
my $who = shift;
print "this is who=>$who\n";
my $items = shift;
my @required = qw(preserver sunscreen water_bottle
jacket);

for my $item (@required) {
unless (grep $item eq $_, @$items) { #if
statement is false
print "$who is missing $item\n";
} #end unless
} #end for
} #end sub

Hope this helps!

Dani


Yes this did the trick. Thanks to all of you who responded. I guess it
is a good practice to use data::dumper when you are developing programs.
When I search CPAN in modules I see a quick snopsis of data dumper. Is
there an area in CPAN that has a more verbose listing of modules?
.



Relevant Pages

  • Re: Self referencing hash
    ... my $class = shift; ... sub type { ... You should also call it something better than HASH because the % ... one of the hash values an anonymous array. ...
    (perl.beginners)
  • code references - how to
    ... I have tried putting junk values in the hash values - sub ... So how do you create a code reference in this context, ... my $root = shift; ...
    (perl.beginners)
  • Re: hash on private member variable
    ... What does it mean to "use hash on a private member variable"? ... > sub new { ... > my $class = shift; ... Sinan Unur ...
    (comp.lang.perl.misc)
  • Multileval data structures, tie, and indirection
    ... A map is one 2D level, ... hash of data about the grid. ... sub TIEARRAY { ... my $class = shift; ...
    (comp.lang.perl)
  • Printing an array within a hash
    ... I am trying to print the contents of the array from within the hash. ... my $all = shift; ... sub check_items_required { ... print "$who is missing $item\n"; ...
    (perl.beginners)