On Aug 22, 12:44 am, [EMAIL PROTECTED] (Chris) wrote:
> #!/usr/bin/perl -w
> use strict;
>
> sub check_items_for_all {
>         my $all_ref = @_;

This assigns $all_ref to be the number of items in @_.  I don't think
that's what you want.  You are passing a reference to a hash as the
first argument.  You need to get the first argument out of @_.  That
means you need one of:
my $all_ref = shift;
my $all_ref = $_[0];
(depending on whether or not you want @_ destroyed in the process)

>         my @who = keys %$all_ref;
>         my @required  = qw(preserver sunscreen water_bottle jacket);
>         my @missing = ( );
>
>         for my $crew (@who){
>                 for my $item(@required){
>                         unless (grep $item eq $_, %$all_ref{$crew}) {

%$all_ref{$crew} does not make sense.  I don't know what you're trying
to achieve there.  $all_ref is a reference to a hash whose values are
references to arrays.  That is:
$all_ref  ==> reference to a hash
%{$all_ref} ==> the hash $all_ref references
${$all_ref}{$crew}  ==> one element of $all_ref, a reference to an
array
$all_ref->{$crew} ==> Syntactic sugar.  Same thing as above.
@{$all_ref->{$crew}} ==> the array that $all_ref->{$crew} references.

The second problem (which isn't really a problem) is your syntax for
grep.  There are two possible syntaxes for grep().  They are:
grep EXPR, LIST
grep BLOCK LIST

You'll note that the first one does have a comma following the
expression.  The second one does not.  I personally prefer the second
one, because the block syntax makes it explicit where the precedence
lies, whereas I'd be second guessing myself using the expression
syntax.  So I'd rewrite your line as:

unless (grep { $item eq $_ } @{$all_ref->{$crew}} ) {


>                                 print "$crew is missing $item.\n";
>                                 #push @missing, $item;
>                         }
>                 }
>         }
>
>         #if (@missing) {
>         #       print "Adding @missing to @$items for $who.\n";
>         #       push @$items, @missing;
>         #}
>
> }
>
> my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
> my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
> my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
>
> my %all = (
>         Gilligan => [EMAIL PROTECTED],
>         Skipper => [EMAIL PROTECTED],
>         Professor =>  [EMAIL PROTECTED],
> );
>
> check_items_for_all(\%all);
>
> When I run the script, I get the following error message:
> syntax error at required_items.pl line 13, near "$all_ref{"
> syntax error at required_items.pl line 18, near "}"

99% sure those syntax errors are a result of your failed dereferencing
of $all_ref.

You should probably read up on references and multi-level structures
in Perl:
perldoc perlreftut
perldoc perllol
perldoc perldsc

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to