Hope this helps you...

#!/usr/bin/perl -w
#  formatted with perltidy

use warnings;    # I always use this
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
{
   foreach my $person ( @crew )
   {
      my $who  = ${ $person }{ 'name' };
      my @keys = keys %required_list;

      foreach ( @keys )
      {
         # Two variables to see it clearly
         my $a1 = $required_list{ "$_" };
         my $a2 = ${ $person }{ "$_" };

         # Initialize $a2 if it's missing, to avoid warning
         $a2 = " " if not $a2;

         unless ( $a1 eq $a2 )
         {
            print "$who is missing $_\n";
         }
      } ## end foreach ( @keys )

   } ## end foreach my $person ( @crew ...
} ## end sub check_required_items


On Sun, May 3, 2009 at 1:53 PM, Thomas H. George <li...@tomgeorge.info>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.

Reply via email to