1 1ddd2d4e 2023-09-10 jrmu #!/usr/bin/perl
3 1ddd2d4e 2023-09-10 jrmu # The Professor's data file (mentioned earlier in this chapter) is
4 1ddd2d4e 2023-09-10 jrmu # available as coconet.dat in the Download section of
5 1ddd2d4e 2023-09-10 jrmu # http://www.intermediateperl.com/. There may be comment lines (beginning
6 1ddd2d4e 2023-09-10 jrmu # with a #); be sure to skip them. (That is, your program should skip them.
7 1ddd2d4e 2023-09-10 jrmu # You might find a helpful hint if *you* read them!) Here are the first
8 1ddd2d4e 2023-09-10 jrmu # data lines in the file:
10 1ddd2d4e 2023-09-10 jrmu # (see 5.References.And.Scoping/coconet.dat)
12 1ddd2d4e 2023-09-10 jrmu # Modify the code from this chapter so that each source machine's portion
13 1ddd2d4e 2023-09-10 jrmu # of the output shows the total bytes from that machine. List the source
14 1ddd2d4e 2023-09-10 jrmu # machines in order from most to least data transferred. Within each group,
15 1ddd2d4e 2023-09-10 jrmu # list the destination machines in order from most to least data
16 1ddd2d4e 2023-09-10 jrmu # transferred to that target from the source machine:
18 1ddd2d4e 2023-09-10 jrmu # professor.hut => gilligan.hut: 1845
19 1ddd2d4e 2023-09-10 jrmu # professor.hut => maryann.hut: 90
21 1ddd2d4e 2023-09-10 jrmu # The result should be that the machine that sent the most data will be the
22 1ddd2d4e 2023-09-10 jrmu # first source machine in the list, and the first destination should be the
23 1ddd2d4e 2023-09-10 jrmu # machine to which it sent the most data. The Professor can use this
24 1ddd2d4e 2023-09-10 jrmu # printout to reconfigure the network for efficiency.
27 1ddd2d4e 2023-09-10 jrmu use warnings;
32 1ddd2d4e 2023-09-10 jrmu while (<>) {
33 1ddd2d4e 2023-09-10 jrmu next if (/\A\s*#/);
34 1ddd2d4e 2023-09-10 jrmu my ($src, $dst, $bytes) = split;
35 1ddd2d4e 2023-09-10 jrmu $hosts{$src}{$dst} = $bytes;
38 1ddd2d4e 2023-09-10 jrmu my $hashref = shift;
40 1ddd2d4e 2023-09-10 jrmu foreach (keys %{$hashref}) {
41 1ddd2d4e 2023-09-10 jrmu $total += $hashref->{$_};
43 1ddd2d4e 2023-09-10 jrmu return $total;
45 1ddd2d4e 2023-09-10 jrmu foreach my $src (sort { sum($hosts{$b}) <=> sum($hosts{$a}) } keys %hosts) {
46 1ddd2d4e 2023-09-10 jrmu print "Total bytes ($src): ". sum($hosts{$src}) ."\n";
47 1ddd2d4e 2023-09-10 jrmu foreach my $dst (sort { $hosts{$src}{$b} <=> $hosts{$src}{$a} }
48 1ddd2d4e 2023-09-10 jrmu keys %{$hosts{$src}}) {
49 1ddd2d4e 2023-09-10 jrmu print "$src => $dst $hosts{$src}{$dst}\n";