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

Re: intermix list of items

by tstock (Curate)
on Oct 16, 2004 at 05:11 UTC ( #399727=note: print w/ replies, xml ) Need Help??


in reply to intermix list of items

thanks all. I went with the following based on your feedback -

use strict; my @targets = ('10.1.1.1', '10.1.1.2', '10.2.2.1', '10.2.2.2'); my @list = intermix( @targets ); use Data::Dumper; print Dumper \@list; sub intermix { my @list = @_; my %hash; # separating hash my @ret; # returning list # build hash structure, each similar first 3 octects as key # array reference w/ IP addresses as values for (@list) { push( @{ $hash{ substr($_,0,rindex($_,'.')) } }, $_ ); } my @keys = keys %hash; while (@keys) { push(@keys, shift(@keys)); # rotate keys # extract one element, or delete the array ref and try again my $item = shift @{$hash{ $keys[0] }} || shift @keys && next; push @ret, $item; } return @ret; } __END__ $VAR1 = [ '10.1.1.1', '10.2.2.1', '10.1.1.2', '10.2.2.2' ];
This doesn't require a specially defined sequential list type. I'm sorry it doesn't look small and neat though.

Tiago


Comment on Re: intermix list of items
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2015-07-05 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls