Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How to get this not the usual round robin looping

by AnomalousMonk (Archbishop)
on Oct 16, 2015 at 12:44 UTC ( [id://1145110]=note: print w/replies, xml ) Need Help??


in reply to How to get this not the usual round robin looping

Here's a variation on hdb's (nice++) approach that uses List::MoreUtils::part():

c:\@Work\Perl\monks\fidda>perl -wMstrict -le "use List::MoreUtils qw(part); ;; use constant KEYS => qw(H1 H2 H3 H4); use constant ITEMS => 'A' .. 'O'; ;; my @steps = map { @$_, reverse @$_ } [ 0 .. KEYS-1 ]; ;; my $i = 0; my @parts = part { $steps[ $i++ % @steps ] } ITEMS; ;; my %h; @h{ KEYS() } = @parts; for my $k (KEYS) { printf qq{$k }; printf qq{'$_' } for @{ $h{$k} }; print ''; } " H1 'A' 'H' 'I' H2 'B' 'G' 'J' 'O' H3 'C' 'F' 'K' 'N' H4 'D' 'E' 'L' 'M'
If you don't like part(), you can use
    my @parts;
    push @{ $parts[ $steps[ $i++ % @steps ] ] }, $_ for ITEMS;

I kinda don't like the stray  $i variable wandering through the code, but if you wrap this all up in a distributor function, this is no longer an issue.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found