Blob


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