Home > Archive > PERL Beginners > September 2006 > Hash problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Reginald Johnson 2006-09-28, 6:57 pm |
| 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
| |
| Chen Li 2006-09-28, 6:57 pm |
|
--- "Johnson, Reginald (GTI)" <reggie_johnson@ml.com>
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
| |
| D. Bolliger 2006-09-28, 6:57 pm |
| 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
| |
| John W. Krahn 2006-09-28, 6:57 pm |
| 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
| |
| Reginald Johnson 2006-09-28, 6:57 pm |
|
-----Original Message-----
From: D. Bolliger [mailto:info@dbolliger.ch]=20
Sent: Thursday, September 28, 2006 4:32 PM
To: beginners@perl.org
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=3D>HASH(0x20040014)
> this is who=3D>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 =3D qw(red_shirt hat lucky_socks water_bottle);
> my @skipper =3D qw ( blue_shirt hat preserver sunscreen);
> my @professor =3D qw(sunscreen water_bottle slide_rule =
batteries
> radio);
>
> my %all =3D {
usage of '()' instead of '{}' would probably help :-)
The hash as defined in your line has one key with a stringified address,
and=20
the value is undef.
Check this out with
use Data::Dumper;
warn Data::Dumper::Dumper (\%all);
> "Gilligan" =3D> \@gilligan,
> "Skipper" =3D> \@skipper,
> "Professor" =3D> \@professor,
> };
>
> check_items_for_all(\%all);
>
> sub check_items_for_all{
> my $all =3D shift;
> for my $person(sort keys %$all) {
> print "this is person=3D>$person\n";
> check_items_required($person,
$all->{$person});
> } #end for
> } #end check_items_for_all
>
> sub check_items_required {
> my $who =3D shift;
> print "this is who=3D>$who\n";
> my $items =3D shift;
> my @required =3D 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?
| |
| D. Bolliger 2006-09-28, 6:57 pm |
| Johnson, Reginald (GTI) am Donnerstag, 28. September 2006 22:56:
> I guess it
> is a good practice to use data::dumper when you are developing programs.
What you should always use is (as others pointed out) the lines:
use strict;
use warnings;
to improve detection of errors.
Data::Dumper is useful if you are not shure how a data structure looks like (
hm, that should not occur in own programs ;-) ), or to check if it really
looks as expected.
And of course creating test scripts is always a good thing to answer the
question: Does my program what it should do? See for example:
Test::Simple
Test::More
> 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?
I'm not sure what you mean here. You can use search.cpan.org to search
modules, and after a click on the module name, the manual is displayed.
Via this page you can (directly) reach the source code, and (indirectly) all
files in the distribution, including the test scripts.
regards
Dani
| |
| Dr.Ruud 2006-09-28, 6:57 pm |
| "Johnson, Reginald (GTI)" schreef:
> I guess it
> is a good practice to use data::dumper when you are developing
> programs.
ITYM: Data::Dumper (casing matters).
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|