aboutsummaryrefslogtreecommitdiff
path: root/challenge-094/abigail/perl/ch-1.pl
blob: c2fd0ddf8a1dd4cd0927c543f38a6b6cec4076dd (plain)
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
#!/opt/perl/bin/perl

use 5.032;

use strict;
use warnings;
no  warnings 'syntax';

use experimental 'signatures';
use experimental 'lexical_subs';

while (<>) {
    my %anagrams;
    #
    # Iterate over the words. We're assuming all the words are on
    # a single line, and the words don't contain a double quote.
    # (If we can't assume this, we could use a CSV parser instead).
    #
    foreach my $word (/"([^"]+)"/g) {
        #
        # Normalize each word: split into characters, sort them, join them.
        # Note that we're splitting on characters, not graphemes, nor
        # do we normalize the input. This may lead to unexpected results
        # when using accented letters and/or combining characters. But
        # that's what you get when using poor specifications.
        #
        my $normalized = join "" => sort split // => $word;
        push @{$anagrams {$normalized}} => $word;
    }
    #
    # Print them. We make this deterministic, so we can easily write tests
    #
    foreach my $key (sort keys %anagrams) {
        say join ", " => map {qq {"$_"}} @{$anagrams {$key}};
    }
}

__END__