Perl中级第四章课后习题

1.下列表达式各表示什么不同的含义:
$ginger->[2][1]
${$ginger[2]}[1]
$ginger->[2]->[1]
${$ginger->[2]}[1]

除第2个表示数组的元素,其他的都表示相同的意义

4. 9. 2. 练习2 [30 分钟]
运用书中最后那个版本的check_required_items , 写一个子程序
check_items_for_all , 把一个散列引用作为惟一参数.这个散列引用的键是在
Minnow 船上的所有乘客, 其对应的值是他们各自想带上船的东西。
比如, 这个散列引用可以是这样的:
my @gilligan =. . . gilligan items. . . ;
my @skipper =. . . skipper items. . . ;
my @professor =. . . professor items. . . ;
my %all = (
Gilligan => @gilligan,
Skipper => @skipper,
Professor => @professor,
);
check_items_for_all(\%all);
我们新建的那个子程序要调用check_required_items 来为散列中每个人更新其
必需的装备清单。

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 => @gilligan,
    Skipper => @skipper,
    Professor => @professor,
);
check_items_for_all(%all);


sub check_items_for_all {
    my $all = shift ;

    foreach my $person ( keys %{$all} ) {
        check_required_items($person,$all->{$person});

    }


}


sub check_required_items{
    my $who = shift ;
    my $items = shift;

    my @required = qw(preserver sunscreen water_bottle jacket);
    my @missing = ( );

    foreach my $item (@required) {
        unless ( grep $item eq $_ ,@$items ) {
            print "$who is missing $item. 
";
            push @missing,$item;
        }
    }


    if (@missing) {
        print "Adding @missing to @$items for $who. 
";
        push @$items,@missing;
    }


}
原文地址:https://www.cnblogs.com/tjxwg/p/3374052.html