Problems? Is your data what you think it is? | |
PerlMonks |
Re: How to bucket a Hashby Athanasius (Archbishop) |
on Nov 27, 2013 at 06:56 UTC ( [id://1064560]=note: print w/replies, xml ) | Need Help?? |
Hello techman2006, Your question implies that the code shown works correctly. But what happens if you have an odd number of elements in the hash? Running your code with %hash = ( 1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e' ) I get:
which shows that the fifth element is left out altogether. Anyway, here is my solution, a variation on hdb’s approach which uses the natatime function from List::MoreUtils:
Update: Changed 'a' .. 'j' to 'a' .. 'e', and the first argument to natatime from 4 to 2, to match the output shown. Output:
Hope that helps,
In Section
Seekers of Perl Wisdom
|
|