- 论坛徽章:
- 7
|
回复 16# cujeson - sub try {
- return [] if @_ == 0;
- my $a = shift;
- my @R =
- map {
- my $h = $_;
- print "-> $h ";
- map { [ $h, @$_ ] } try(@_)
- } @$a;
- say "$/\@a = @$a return =";
- say "\t@$_ " for @R;
- @R;
- }
- try [qw/A B/], [qw/x y/], [qw/1 2 3 4/];
复制代码 output:- -> A -> x -> 1 -> 2 -> 3 -> 4
- @a = 1 2 3 4 return =
- 1
- 2
- 3
- 4
- -> y -> 1 -> 2 -> 3 -> 4
- @a = 1 2 3 4 return =
- 1
- 2
- 3
- 4
- @a = x y return =
- x 1
- x 2
- x 3
- x 4
- y 1
- y 2
- y 3
- y 4
- -> B -> x -> 1 -> 2 -> 3 -> 4
- @a = 1 2 3 4 return =
- 1
- 2
- 3
- 4
- -> y -> 1 -> 2 -> 3 -> 4
- @a = 1 2 3 4 return =
- 1
- 2
- 3
- 4
- @a = x y return =
- x 1
- x 2
- x 3
- x 4
- y 1
- y 2
- y 3
- y 4
- @a = A B return =
- A x 1
- A x 2
- A x 3
- A x 4
- A y 1
- A y 2
- A y 3
- A y 4
- B x 1
- B x 2
- B x 3
- B x 4
- B y 1
- B y 2
- B y 3
- B y 4
复制代码 in last element [1, 2, 3, 4]
map { my $h = $_; map [ $h, @$_ ], try(@_) } 1, 2, 3, 4
now @_ is empty so sub return []
[ $h, @$_ ] => [ $h, @{ [] } ] => [ $h ] -> [1]
[1]
[2]
[3]
[4]
in [x, y]
map [ $h, @$_ ], [1], [2], [3], [4]
[ $h, @$_ ] => [ x, @{ [1] } ] =>
[x, 1]
[x, 2]
[x, 3]
[x, 4]
....
[y, 4]
in [A, B]
map [ $h, @$_ ], [x, 1],[x, 2] ... [y, 4]
[ $h, @$_ ] => [ A, @{ [x, 1] } ] =>
[A, x, 1]
[A, x, 2]
...
[A, y, 4]
[B, x, 1]
....
[B, y, 4] |
|