in reply to Re: how to push 5th element from one array to another array
in thread how to push 5th element from one array to another array
Your code:
adds a fraction (4/5) to the last array index. So adding appropriate parentheses:my @fifth = @arr[ map {$_*5-1} 1..$#arr+4/5 ];
seems better but it does not work for all array lengths. (It works if the length modulo 5 is 0 or 1 only.) Changing the 4 to a 1 (aka the array length) does the trick:my @fifth = @arr[ map {$_*5-1} 1..($#arr+4)/5 ];
my @fifth = @arr[ map {$_*5-1} 1..($#arr+1)/5 ];
|
---|
In Section
Seekers of Perl Wisdom