Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: how to push 5th element from one array to another array

by gmargo (Hermit)
on Oct 28, 2009 at 02:29 UTC ( #803560=note: print w/replies, xml ) Need Help??


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:

my @fifth = @arr[ map {$_*5-1} 1..$#arr+4/5 ];
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+1)/5 ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://803560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-12-06 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?