Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to iterate by fives?

by zengargoyle (Deacon)
on Feb 17, 2003 at 16:14 UTC ( [id://236000]=note: print w/replies, xml ) Need Help??


in reply to How to iterate by fives?

i didn't see anything like this for chunking up the list. this way makes me warm and fuzzy...

@things = 'a'..'z'; print "#things", $/; print "@things", $/; $by = 5; print "#by", $/; print "$by", $/; @extra = ('.') x $by - 1; print "#extra", $/; print "@extra", $/; @padded_things = (@things, @extra); print "#padded_things", $/; print "@padded_things", $/; print "#chunked", $/; while ( (@chunk = splice @padded_things, 0, $by) == $by ) { print "@chunk", $/; }
$ perl test.pl 
#things
a b c d e f g h i j k l m n o p q r s t u v w x y z
#by
5
#extra
. . . . 
#padded_things
a b c d e f g h i j k l m n o p q r s t u v w x y z  . . . .
#chunked
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z . . . .

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found