Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69224
  • 博文数量: 13
  • 博客积分: 247
  • 博客等级: 二等列兵
  • 技术积分: 138
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-02 18:17
文章分类

全部博文(13)

文章存档

2015年(1)

2014年(1)

2013年(1)

2012年(2)

2011年(8)

我的朋友

分类: 系统运维

2011-08-09 12:19:36

exercise 2:
  1. #! /usr/bin/perl -w
  2. use strict;

  3. my @required = qw(sunscreen water_bottle slide_rule batteries radio);
  4. my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
  5. my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
  6. my @professor = qw(sunscreen water_bottle slide_rule batteries radio);

  7. my %all = (
  8. "gilligan" => \@gilligan,
  9. "skipper" => \@skipper,
  10. "professor" => \@professor,
  11. );

  12. &check_items_for_all (\%all);

  13. sub check_required_items
  14. {
  15.     my $who = shift;
  16.     my $items = shift;
  17.     my @missing = ();
  18.     foreach my $item(@required)
  19.     {
  20.         unless (grep $item eq $_,@$items)
  21.         {
  22.             print "$who is missing $item.\n";
  23.             push @missing,$item;
  24.      }
  25.     }
  26.     if(@missing)
  27.     {
  28.         print "adding @missing to @$items for $who.\n";
  29.         push @$items,@missing;
  30.         }
  31. }

  32. sub check_items_for_all
  33. {
  34.     #####my $hr = shift;
  35.     foreach my $person(sort keys %{$_[0]})
  36.     {
  37.         check_required_items $person,$_[0]->{$person};
  38.     }
  39. }
总结:
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
阅读(1649) | 评论(2) | 转发(0) |
0

上一篇:没有了

下一篇:Intermediate Perl Learning Notes-chapter 5

给主人留下些什么吧!~~

网络安全服务2011-08-16 10:58:50

加油```

regansong2011-08-09 13:07:57

小松加油