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

Re^2: intermix list of items

by ikegami (Patriarch)
on Oct 10, 2004 at 18:42 UTC ( [id://398022]=note: print w/replies, xml ) Need Help??


in reply to Re: intermix list of items
in thread intermix list of items

Here's an algorithm that doesn't suffer from the problem mentioned in the Note above:

my @list = ('10.1.1.[1..2]', '10.2.2.[1..2]', '10.3.3.[3..6]'); my @list_prime = map { my $n = $_; $n =~ s/\[(\d+)\.\.(\d+)\]//; [ $n, $1, $2 ] } @list; my @ordered_list; my $done; do { $done = 1; foreach (@list_prime) { next unless defined($_); if ($_->[1] > $_->[2]) { undef($_); next } push(@ordered_list, $_->[0].($_->[1]++)); $done = 0; } } while (!$done); use Data::Dumper; print Dumper(@ordered_list); __END__ output ====== $VAR1 = '10.1.1.1'; # First from first. $VAR2 = '10.2.2.1'; # First from second. $VAR3 = '10.3.3.3'; # First from third. $VAR4 = '10.1.1.2'; # Second from first. $VAR5 = '10.2.2.2'; # Second from second. $VAR6 = '10.3.3.4'; # Second from third. $VAR7 = '10.3.3.5'; # Third from third. $VAR8 = '10.3.3.6'; # Fourth from third.

It can probably be optimised.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found