Chinaunix首页 | 论坛 | 博客
  • 博客访问: 836079
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-20 14:09:32

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

  4. my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
  5. check_required_items('The Skipper', \@skipper);
  6. print "@skipper\n";
  7. sub check_required_items{
  8.     my $who = shift;
  9.     my $items = shift;
  10.     my @required = qw(preserver sunscreen water_bottle jacket);
  11.     my @missing=();

  12.     foreach my $item (@required){
  13.         unless( grep $item eq $_, @$items){
  14.             print "$who missing $item\n";
  15.             push @missing, $item;
  16.         }
  17.     }
  18.     if (@missing){
  19.         push @$items, @missing;
  20.     }
  21. }
#The Skipper missing water_bottle
#blue_shirt hat jacket preserver sunscreen water_bottle
here we insert the missing items to the orignal array. if dont use the reference, the change will be not affected the orignal array.




阅读(454) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~