http://www.perlmonks.org?node_id=398021


in reply to intermix list of items

btw, the compare function is suppose to return -1, 0, +1, not just 0 and 1, so I expect your sort has some amount of randomness to it.

Maybe you want something like:

sub intermix { my $a_prime = join('.', reverse(split(/\./, $a))); my $b_prime = join('.', reverse(split(/\./, $b))); $a_prime cmp $b_prime } my @list = ('10.1.1.1', '10.1.1.2', '10.2.2.1', '10.2.2.2'); @list = sort intermix @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = '10.1.1.1'; $VAR2 = '10.2.2.1'; $VAR3 = '10.1.1.2'; $VAR4 = '10.2.2.2';

Almost the same, but more efficient:

sub intermix { my $a_prime = reverse($a); my $b_prime = reverse($b); $a_prime cmp $b_prime } my @list = ('10.1.1.1', '10.1.1.2', '10.2.2.1', '10.2.2.2'); @list = sort intermix @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = '10.1.1.1'; $VAR2 = '10.2.2.1'; $VAR3 = '10.1.1.2'; $VAR4 = '10.2.2.2';

This is the same thing as the last, but probably more efficient, especially for longer lists:

my @list = ('10.1.1.1', '10.1.1.2', '10.2.2.1', '10.2.2.2'); @list = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, ''.reverse($_) ] } @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = '10.1.1.1'; $VAR2 = '10.2.2.1'; $VAR3 = '10.1.1.2'; $VAR4 = '10.2.2.2';

Note: Given addresses of the form 'a.b.c.d', the above snippets only work well if there is there is a common 'd' of for every 'a.b.c' in the list.

Replies are listed 'Best First'.
Re^2: intermix list of items
by ikegami (Patriarch) on Oct 10, 2004 at 18:42 UTC

    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.