| 448 | |
| 449 | == ステップつきスライス == |
| 450 | * つまるところ Python での {{{list[start:end:step]}}}. |
| 451 | {{{ |
| 452 | #!perl |
| 453 | my @array = (100 .. 150); |
| 454 | my $step = 3; |
| 455 | |
| 456 | print join(", ", &slice(\@array, 0, $#array, $step)), "\n"; |
| 457 | print join(", ", &slice(\@array, 1, $#array, $step)), "\n"; |
| 458 | print join(", ", &slice(\@array, 2, $#array, $step)), "\n"; |
| 459 | |
| 460 | sub slice($$$$) { |
| 461 | my ($array, $start, $end, $step) = @_; |
| 462 | |
| 463 | my @subs = grep { $_ % $step == $start } ($start .. $end); |
| 464 | |
| 465 | return @{$array}[@subs]; |
| 466 | } |
| 467 | }}} |
| 468 | * 実行結果 |
| 469 | {{{ |
| 470 | 100, 103, 106, 109, 112, 115, 118, 121, 124, 127, 130, 133, 136, 139, 142, 145, 148 |
| 471 | 101, 104, 107, 110, 113, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149 |
| 472 | 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150 |
| 473 | }}} |