Blame


1 1ddd2d4e 2023-09-10 jrmu #!/usr/bin/perl
2 1ddd2d4e 2023-09-10 jrmu
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:
9 1ddd2d4e 2023-09-10 jrmu #
10 1ddd2d4e 2023-09-10 jrmu # (see 5.References.And.Scoping/coconet.dat)
11 1ddd2d4e 2023-09-10 jrmu #
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:
17 1ddd2d4e 2023-09-10 jrmu #
18 1ddd2d4e 2023-09-10 jrmu # professor.hut => gilligan.hut: 1845
19 1ddd2d4e 2023-09-10 jrmu # professor.hut => maryann.hut: 90
20 1ddd2d4e 2023-09-10 jrmu #
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.
25 1ddd2d4e 2023-09-10 jrmu
26 1ddd2d4e 2023-09-10 jrmu use v5.24;
27 1ddd2d4e 2023-09-10 jrmu use warnings;
28 1ddd2d4e 2023-09-10 jrmu use strict;
29 1ddd2d4e 2023-09-10 jrmu use utf8;
30 1ddd2d4e 2023-09-10 jrmu
31 1ddd2d4e 2023-09-10 jrmu my %hosts;
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;
36 1ddd2d4e 2023-09-10 jrmu }
37 1ddd2d4e 2023-09-10 jrmu sub sum {
38 1ddd2d4e 2023-09-10 jrmu my $hashref = shift;
39 1ddd2d4e 2023-09-10 jrmu my $total;
40 1ddd2d4e 2023-09-10 jrmu foreach (keys %{$hashref}) {
41 1ddd2d4e 2023-09-10 jrmu $total += $hashref->{$_};
42 1ddd2d4e 2023-09-10 jrmu }
43 1ddd2d4e 2023-09-10 jrmu return $total;
44 1ddd2d4e 2023-09-10 jrmu }
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";
50 1ddd2d4e 2023-09-10 jrmu }
51 1ddd2d4e 2023-09-10 jrmu }