- 论坛徽章:
- 7
|
本帖最后由 rubyish 于 2013-12-01 19:35 编辑
回复 4# cujeson
line 8: recursive cartesian product sub, 只可意会,不可以言传也。
千言万语,都不能说清楚了。
http://tv.sohu.com/20130711/n381345559.shtml
usage:- my @a = try [ 1, 2 ], [ 'a', 'b' ], [ 'A', 'B' ];
- say join ' | ', map "@$_", @a;
- # 1 a A | 1 a B | 1 b A | 1 b B | 2 a A | 2 a B | 2 b A | 2 b B
复制代码 a simple version: try2
perldoc -f glob- sub try2 {
- my ( $head, $tail ) = qw[{ }];
- my @param = @_;
- my @body = map { join( '|,', @$_) .'|' } @param;
- my $body = join '}{', @body;
- my @all = glob $head . $body . $tail;
- my @ret = map { [ split /\|/, $_ ] } @all;
- return @ret;
- }
复制代码 try2_oneline = try2- sub try2_oneline {
- map [ split /\|/ ], glob '{' . join( '}{', map join( '|,', @$_ ) . '|', @_ ) . '}';
- }
复制代码 line 12-19: 句型 print ... for @A
- @A: 句型 sort {...} @B
- @B: 句型 map {...} @C
- @C: 句型 try @D
- @D: 句型 map {...} @cut
line 12-19 =- print ... for sort {...} map {...} try map {...} @cut
复制代码 EQV- my @D = map {...} @cut;
- my @C = try @D;
- my @B = map {...} @C;
- my @A = sort {...} @B;
- print ... for @A;
复制代码- @D: [ 0, 1, 2, 3, 4 ], [ 0, 1, 2, 3 ], [ 0, 1, 2, 3, 4, 5 ]
- @C: [ 0, 0, 0 ], [ 0, 0, 1 ], [ 0, 0, 2 ] ... [ 4, 3, 5 ]
- @B: [ 9.5, 0.50, 0, 0, 5 ], [ 8.3, 1.70, 0, 1, 3 ] ... [ 10, 0.00, 4, 0, 0 ]
- @A: [ 10, 0.00, 4, 0, 0 ], [ 9.7, 0.30, 0, 3, 1 ] ... [ 8.2, 1.80, 1, 0, 3 ]
line15: sum = 2.5 * a + 2.6 * b + 1.9 * c ....
line 17-18: true ? A : B- my @even = map { $_ % 2 == 0 ? "E$_" : () } 1 .. 10;
- my @eve2 = map { "E$_" } grep { $_ % 2 == 0 } 1 .. 10;
- say join '|', @even;
- # E2|E4|E6|E8|E10
- say join '|', @eve2;
- # E2|E4|E6|E8|E10
复制代码 |
|