1 1ddd2d4e 2023-09-10 jrmu #!/usr/bin/perl
3 1ddd2d4e 2023-09-10 jrmu # Starting with your data structure from Exercise 2, rewrite the
4 1ddd2d4e 2023-09-10 jrmu # coconet.dat file so that it's in the same format, but sorted by source
5 1ddd2d4e 2023-09-10 jrmu # machine. Report each destination machine once per source machine along
6 1ddd2d4e 2023-09-10 jrmu # with total bytes transferred. The destination machines should be indented
7 1ddd2d4e 2023-09-10 jrmu # under the source machine name and be sorted by the machine name:
10 1ddd2d4e 2023-09-10 jrmu # maryann.hut 13744
11 1ddd2d4e 2023-09-10 jrmu # professor.hut
12 1ddd2d4e 2023-09-10 jrmu # gilligan.hut 1845
13 1ddd2d4e 2023-09-10 jrmu # maryann.hut 90
14 1ddd2d4e 2023-09-10 jrmu # thurston.howell.hut
15 1ddd2d4e 2023-09-10 jrmu # lovey.howell.hut 97560
19 1ddd2d4e 2023-09-10 jrmu use warnings;
24 1ddd2d4e 2023-09-10 jrmu while (<>) {
25 1ddd2d4e 2023-09-10 jrmu next if (/\A\s*#/);
26 1ddd2d4e 2023-09-10 jrmu my ($src, $dst, $bytes) = split;
27 1ddd2d4e 2023-09-10 jrmu $hosts{$src}{$dst} = $bytes;
30 1ddd2d4e 2023-09-10 jrmu my $hashref = shift;
32 1ddd2d4e 2023-09-10 jrmu foreach (keys %{$hashref}) {
33 1ddd2d4e 2023-09-10 jrmu $total += $hashref->{$_};
35 1ddd2d4e 2023-09-10 jrmu return $total;
37 1ddd2d4e 2023-09-10 jrmu foreach my $src (sort { sum($hosts{$b}) <=> sum($hosts{$a}) } keys %hosts) {
38 1ddd2d4e 2023-09-10 jrmu print "$src\n";
39 1ddd2d4e 2023-09-10 jrmu foreach my $dst (sort { $hosts{$src}{$b} <=> $hosts{$src}{$a} }
40 1ddd2d4e 2023-09-10 jrmu keys %{$hosts{$src}}) {
41 1ddd2d4e 2023-09-10 jrmu print " $dst $hosts{$src}{$dst}\n";