Re: Hash problem





--- "Johnson, Reginald (GTI)" <reggie_johnson@xxxxxx>
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 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,
};

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


Reggie Johnson
TSM Admin
Hi,

1) add line use warnings; after line use strict;

2) change %all( which is a hash) into $all_ref(which
is a hash reference)

3) Are the following what you expect?
###output #########

this is person=>Gilligan
this is who=>Gilligan
Gilligan is missing preserver
Gilligan is missing sunscreen
Gilligan is missing jacket
this is person=>Professor
this is who=>Professor
Professor is missing preserver
Professor is missing jacket
this is person=>Skipper
this is who=>Skipper
Skipper is missing water_bottle
Skipper is missing jacket

Li

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
.



Relevant Pages

  • RE: Hash problem
    ... My hash is showing the address instead of the name and I'm not sure ... HASHis missing preserver ... my $all = shift; ... sub check_items_required { ...
    (perl.beginners)
  • Re: Hash problem
    ... My hash is showing the address instead of the name and I'm not sure ... HASHis missing preserver ... If you had included the warnings pragma then perl would have told you what is ... Reference found where even-sized list expected at Hash_problem.pl line 8. ...
    (perl.beginners)
  • Re: Hash#shift
    ... On 14.05.2007 14:59, Harry Kakueki wrote: ... I was reading this http://www.rubycentral.com/book/ref_c_hash.html#Hash.shift ... I must be missing something. ... I never had use for this myself but it might be useful in cases where you want to iterate through a Hash and make sure that elements that you have processed are removed. ...
    (comp.lang.ruby)
  • Re: Ogle / DVD playback
    ... Hash: SHA1 ... it doesn't look like I'm setup right there. ... I'm not there yet - and probably missing the obvious. ...
    (Debian-User)
  • Re: Question re: nfs
    ... Hash: SHA1 ... > obviously missing something - and probably something obvious. ... Version: GnuPG v1.2.4 (GNU/Linux) ...
    (Debian-User)