diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2023-03-22 08:10:28 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2023-03-22 08:10:28 -0600 |
| commit | 83c45a8adb310338def62750bf66cb461bfbb52a (patch) | |
| tree | fd7a7e81da142d63a7380dd5687e856ac49dc3ee | |
| parent | 0f80fec7fbf53a1da397910e6c95da28ba79ef7b (diff) | |
| download | perlweeklychallenge-club-83c45a8adb310338def62750bf66cb461bfbb52a.tar.gz perlweeklychallenge-club-83c45a8adb310338def62750bf66cb461bfbb52a.tar.bz2 perlweeklychallenge-club-83c45a8adb310338def62750bf66cb461bfbb52a.zip | |
Fix yet another error
| -rwxr-xr-x | challenge-209/wlmb/perl/ch-2.pl | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/challenge-209/wlmb/perl/ch-2.pl b/challenge-209/wlmb/perl/ch-2.pl index 46ad3198c8..ff3285d04a 100755 --- a/challenge-209/wlmb/perl/ch-2.pl +++ b/challenge-209/wlmb/perl/ch-2.pl @@ -5,20 +5,24 @@ # See https://wlmb.github.io/2023/03/20/PWC209/#task-2-merge-account use v5.36; use English; +use List::Util qw(uniq); my %line_of; +my %addresses_of; my @names; while(<>){ chomp; - my ($name, @addresses)=split / /; + # Assume input is of the form: name address1 address2... + my ($name, @addresses)=split ' '; next unless $name; # skip empty lines $names[$INPUT_LINE_NUMBER]=$name; - my ($merged)=grep {defined $_} map {$line_of{$_}} @addresses; - $merged//=$INPUT_LINE_NUMBER; # current line by default - @line_of{@addresses}=($merged) x @addresses; + my @merged=grep {defined $_} map {$line_of{$_}} @addresses; # lines to merge with current + push @addresses, map {@{$addresses_of{$_}}} @merged; # add their addresses + @addresses=uniq @addresses; # avoid repetitions + delete $addresses_of{$_} for @merged; # delete merged lines + @line_of{@addresses}=($INPUT_LINE_NUMBER) x @addresses; # map addresses to line + $addresses_of{$INPUT_LINE_NUMBER}=[@addresses]; # map line to addresses } -my %accounts; -push @{$accounts{$line_of{$_}}}, $_ for(keys %line_of); -# Sort output by account name and line number, and sort addresses +# Output. Sort by account name and line number, and sort addresses say "$names[$_] ($_): ", join " ", - sort {$a cmp $b} @{$accounts{$_}} - for sort {$names[$a] cmp $names[$b] || $a <=>$b} keys %accounts; + sort {$a cmp $b} @{$addresses_of{$_}} + for sort {$names[$a] cmp $names[$b] || $a <=>$b} keys %addresses_of; |
