1 2 3 4 5 6 7 8 9
my @nodes = ( "abc.subdomain1.wasweissich.de","def.subdomain1.another.org","ghi.wasweissich.de","abc.subdomain2.wasweissich.de","def.subdomain1.wasweissich.de","ghi.subdomain2.a nother.org","xyz.another.org" ); my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, uc( (/\w+\.(\w+\.\w+)/)[0]) ] } @nodes; print "$_\n" foreach @sorted;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
my @nodes = ( "abc.subdomain1.wasweissich.de","def.subdomain1.another.org","ghi.wasweissich.de","abc.subdomain2.wasweissich.de","def.subdomain1.wasweissich.de","ghi.subdomain2.another.org","xyz.another.org" );
my @sorted = map { $_->[0] }
sort { compare( $a->[1], $b->[1] ) }
map { [ $_, [ reverse( split /\./, $_ ) ] ] } @nodes;
print "$_\n" foreach @sorted;
sub compare {
my( $x, $y ) = @_;
for my $i ( 0 .. ( @$x > @$y ? @$x : @$y ) ) {
my $c = $x->[$i] cmp $y->[$i];
return $c unless $c == 0;
}
return 1 if @$x > @$y;
return -1 if @$y > @$x;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ cat nodes.txt
db.blah.org
de.yahoo.com
mail.example.org
www.aol.com
www.example.org
www.freshmeat.net
www.hotdoggie.com.au
www.blah.org
$ cat nodes.txt | perl -ple'$_=join".",reverse split/\./' | sort | perl -ple'$_=join".",reverse split/\./'
www.hotdoggie.com.au
www.aol.com
de.yahoo.com
www.freshmeat.net
db.blah.org
www.blah.org
mail.example.org
www.example.org
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#! /usr/bin/perl use strict; use warnings; my @nodes = qw( db.blah.org de.yahoo.com mail.example.org www.aol.com www.example.org www.freshmeat.net www.hotdoggie.com.au www.blah.org ); # Schwartzian Transform my @sorted = map { $_->[0] } # sort by domain sort { $a->[1] cmp $b->[1] } # "extract" domain.tld map { [ $_, join '.', (split m/\./)[-2,-1] ] } @nodes; # check result { local $, = local $\ = $/; print @sorted; }
2012-01-24T12:18:59 tobyund dieses "local $, = local $\ = $/;" bei check result verstehe ich nicht.
say for @sorted;
2012-01-24T13:34:27 LinuxerMit [-1] wird das letzte Element angesprochen, mit [-2,-1] die letzten beiden.