in reply to Code challenge: array subrange
You may want to do something other than return an empty list on failure, but you get the idea.use strict; use warnings; print join(" ", array_range(['cero', 'uno', 'dos', 'tres'], [5, 8], [6, 5]) ), "\n"; sub array_range { my ($array, $range, $subrange) = @_; return () unless scalar(@$range) == 2; return () unless scalar(@$subrange) == 2; return () unless scalar(@$array) == $range->[1] - $range->[0] + 1; my $ascending = $subrange->[0] < $subrange->[1]; my @subrange = map {$_-$range->[0]} sort {$a <=> $b} @$subrange; my @subarray = @{$array}[ $subrange[0] .. $subrange[1] ]; return $ascending == 1 ? @subarray : reverse @subarray; }
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
|
|---|