#!/usr/bin/perl # Starting with your data structure from Exercise 2, rewrite the # coconet.dat file so that it's in the same format, but sorted by source # machine. Report each destination machine once per source machine along # with total bytes transferred. The destination machines should be indented # under the source machine name and be sorted by the machine name: # # ginger.hut # maryann.hut 13744 # professor.hut # gilligan.hut 1845 # maryann.hut 90 # thurston.howell.hut # lovey.howell.hut 97560 # ... use v5.24; use warnings; use strict; use utf8; my %hosts; while (<>) { next if (/\A\s*#/); my ($src, $dst, $bytes) = split; $hosts{$src}{$dst} = $bytes; } sub sum { my $hashref = shift; my $total; foreach (keys %{$hashref}) { $total += $hashref->{$_}; } return $total; } foreach my $src (sort { sum($hosts{$b}) <=> sum($hosts{$a}) } keys %hosts) { print "$src\n"; foreach my $dst (sort { $hosts{$src}{$b} <=> $hosts{$src}{$a} } keys %{$hosts{$src}}) { print " $dst $hosts{$src}{$dst}\n"; } }