exercise 2:
- #! /usr/bin/perl -w
- use strict;
- my @required = qw(sunscreen water_bottle slide_rule batteries radio);
- 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_required_items
- {
- my $who = shift;
- my $items = shift;
- my @missing = ();
- foreach my $item(@required)
- {
- unless (grep $item eq $_,@$items)
- {
- print "$who is missing $item.\n";
- push @missing,$item;
- }
- }
- if(@missing)
- {
- print "adding @missing to @$items for $who.\n";
- push @$items,@missing;
- }
- }
- sub check_items_for_all
- {
- #####my $hr = shift;
- foreach my $person(sort keys %{$_[0]})
- {
- check_required_items $person,$_[0]->{$person};
- }
- }
总结:
1.print提供列表环境,array和hash会自动展开成其元素列表相当于join($LIST_SEPARATOR,@array)or
join($",@array);
2.使用引用的一个最重要目的是:decoupling the code from the complex data structure on which it operates and reuse code
阅读(1657) | 评论(2) | 转发(0) |