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

Re: breaking an array into nearly equal parts

by Mandrake (Chaplain)
on Dec 14, 2005 at 09:25 UTC ( [id://516565]=note: print w/replies, xml ) Need Help??


in reply to breaking an array into nearly equal parts

This works for me.
#!/usr/bin/perl -w use strict; my @arr = (1..15) ; my ($offset,$offset1) ; $offset = $offset1 = int((scalar @arr)/3); $offset++ if (scalar @arr % 3 > 0) ; $offset1++ if (scalar @arr % 3 > 1) ; my ($arr1, $arr2, $arr3) = ([@arr[0..$offset-1]],[@arr[$offset..$offset+$offset1-1]],[@arr[$offse +t+$offset1..scalar @arr -1]]); print $_."\n" for ((@$arr1,("\n"),@$arr2),("\n"),@$arr3);
A bit lengthy but you can try refactoring it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found