分类:
2008-07-11 11:11:37
my @odd_digit_sums = @input_numbers[ @indices_of_odd_digit_sums ];
my @bigger_indices = grep { if ($_ > $#y or $x[$_] > $y[$_]) { 1; # yes, select it } else { 0; # no, don't select it } } 0..$#x; my @bigger = @x[@bigger_indices];
my @bigger_indices = grep { $_ > $#y or $x[$_] > $y[$_]; } 0..$#x; my @bigger = @x[@bigger_indices];
my @bigger = map { if ($_ > $#y or $x[$_] > $y[$_]) { $x[$_]; } else { ( ); } } 0..$#x;
my %provisions = ( 'The Skipper' => [qw(blue_shirt hat jacket preserver sunscreen)], 'The Professor' => [qw(sunscreen water_bottle slide_rule radio) ], 'Gilligan' => [qw(red_shirt hat lucky_socks water_bottle) ], );
my @packed_light = grep @{ $provisions{$_} } < 5, keys %provisions;
my @all_wet = grep { my @items = @{ $provisions{$_} }; grep $_ eq 'water_bottle', @items; #这里grep返回的其实仍然是数组,只是外层的grep需要一个scalar的参数,所以数组在scalar的context下就变成了数组元素的个数 } keys %provisions;以上一段代码其实是下面这个更清晰版本的简写版my @all_wet = grep { my @items = @{ $provisions{$_} }; @water=grep $_ eq 'water_bottle', @items;scalare @water; #grep的第一个参数必须返回一个boolean值} keys %provisions;以下没看懂~my @remapped_list = map { [ $_ => $provisions{$_} ]; #这是一种奇怪的用法,匿名数组里面用=>符号,其实改成[$_,$provisions{$_}];一样 } keys %provisions;my @person_item_pairs = map { my $person = $_; my @items = @{ $provisions{$person} }; map [$person => $_], @items;#这里等价于@temp=map [$person => $_], @items;@temp;} keys %provisions;