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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Deep;
is_deeply ( sort_data('../data.txt'),
[ 'user@alpha.example.org',
'rjt@cpan.org',
'rjt@CPAN.org',
'Name@example.org',
'name@example.org',
],
'regular sort' );
is_deeply ( sort_data('../data.txt', '-u'),
[ 'user@alpha.example.org',
'rjt@CPAN.org',
'Name@example.org',
'name@example.org',
],
'unique sort' );
done_testing;
sub sort_data {
my ($file, $flag) = @_;
die "ERROR: Missing data file.\n"
unless defined $file;
die "ERROR: Invalid flag [$flag].\n"
if (defined $flag && $flag !~ /^\-u$/);
open (my $F, "<:encoding(utf8)", $file)
or die "ERROR: Can't open $file: $!\n";
my @source = ();
my $source = ();
while (my $row = <$F>) {
chomp $row;
my ($mailbox, $domain) = split /\@/, $row, 2;
push @source, [$mailbox, $domain];
if ($flag) {
$source->{$mailbox} = $domain;
}
}
close($F);
my $sorted = [];
if ($flag) {
foreach (sort { lc $source->{$a} cmp lc $source->{$b} } sort keys %$source) {
push @$sorted, join "@", $_, $source->{$_};
}
}
else {
foreach (reverse sort { lc $a->[0] cmp lc $b->[0] || $a->[1] cmp $b->[1] } @source) {
push @$sorted, join "@", @$_;
}
}
return $sorted;
}
|