Thomas H. George wrote:
I am inching my way through INTERMEDIATE PERL and tried using hashes to
solve Chapter 4, Problem 2.  I never used hashes before but my attempted
solution convinced me of their value as contrasted to the solution with
arrays.

The Problem:  My solution works but with an error I can't eliminate.

The difficulty is in the next to last line, the 'unless ...' statement
which causes warning:
Use of uninitialized value in string eq at chap4-take6.pl line 49.

Here is the complete program:


#!/usr/bin/perl -w
#
use strict;

my %required_list = ( life_jacket => 'preserver',
        lotion => 'sunscreen',
        water => 'water_bottle',
        coat => 'jacket',
);

my %captain = (
        name => 'skipper',
        life_jacket => 'preserver',
        lotion => 'sunscreen',
        water => 'water_bottle',
        shirt => 'blue_shirt',
        coat => 'jacket',
        hat => 'cap',
);

my %mate = (
        name => 'gilligan',
        shirt => 'red_shirt',
        hat => 'cap',
        socks => 'lucky_socks',
        water => 'water_bottle',
);

my %passenger = (
        name => 'professor',
        lotion => 'sunscreen',
        water => 'water_bottle',
        calculator => 'slide_rule',
        power_supply => 'batteries',
       entertainment => 'radio',
);

my @crew = (\%captain, \%mate, \%passenger);


check_required_items (@crew);

sub check_required_items {

You are using a subroutine and passing a list to it but you never use that list inside the subroutine. In fact, you are accessing the array @crew and the hash %required_list from outside the subroutine, so is there any good reason to use a subroutine at all?


        foreach my $person (@crew) {
                my $who = ${$person} {'name'};

That is usually written as:

                my $who = $person->{ name };


my @keys = keys %required_list; foreach (@keys) {
                        unless ($required_list {"$_"} eq ${$person} {"$_"}) {

You don't need to quote scalar variables:

perldoc -q quoting

Instead of comparing every %required_list key to %$person using 'eq' you should use exists() to check for the existence of those keys.

perldoc -f exists

And, ${$person} {"$_"} is usually written as $person->{ $_ }


                                print "$who is missing $_\n";
                        }       
                }
        }
}

So you want something like:


my @crew = \( %captain, %mate, %passenger );


check_required_items( \%required_list, @crew );


sub check_required_items {
    my ( $req_list, @crew ) = @_;

    for my $person ( @crew ) {

        for my $required ( keys %$req_list ) {
            unless ( exists $person->{ $required } ) {
                print "$person->{ name } is missing $required\n";
                }
            }
        }
    }





John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to