続・5章 練習問題

5-1

実行せずに、このプログラムのまずいところがわかりますか。

tr/{}/()/

5-2

データファイルの処理。転送データの多い順にソースマシンが並ぶようにし、ソースマシングループ内ではソースマシンからの転送量の多いものから順にデスティネーションマシンが並ぶようにする。データ内のコメント行は飛ばす。

#!/usr/bin/perl
use strict;
use warnings;

open DATA, "<coconet.dat" or die "can't open dat file: $!";

my %total_bytes;
my %all;
while (<DATA>) {
    next if /^#/;
    my ( $source, $destination, $bytes ) = split;
    $total_bytes{$source}{$destination} += $bytes;
    $all{$source} += $bytes;
}

for my $source ( sort { $all{$b} <=> $all{$a} } keys %all ) {
    for my $destination (
        sort {
            $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a}
        } keys %{ $total_bytes{$source} } ) {
        printf "%s => %s: %g\n",
            $source,
            $destination,
            $total_bytes{$source}{$destination};
    }
    print "\n";
}

今までで一番悩んだ。しかも、さんざん悩んでたときは答え浮かばなかったのに、違うことしてたときにふと答えが浮かんだ。