開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の5章(リファレンスとスコープ)の5.9(練習問題)2を解いてみる。
その他参考書籍
2.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
my %total_bytes;
my $all = "all";
while (<>) {
next if /^#/;
my ($source, $destination, $bytes) = split;
$total_bytes{$source}{$destination} += $bytes;
$total_bytes{$source}{$all} += $bytes;
}
for my $src (sort {
$total_bytes{$b}{$all} <=> $total_bytes{$a}{$all}
} keys %total_bytes) {
my $tb = $total_bytes{$src};
printf "$src: $tb->{$all} bytes\n";
for my $dst (sort {$tb->{$b} <=> $tb->{$a}} keys %$tb) {
next if $dst eq $all;
printf " => $dst: $tb->{$dst} bytes\n";
}
}
入出力結果(Terminal)
$ ./sample.pl coconet.dat
lovey.howell.hut: 1139833 bytes
=> laser3.copyroom.hut: 212456 bytes
=> professor.hut: 175065 bytes
=> ginger.girl.hut: 158084 bytes
=> maryann.girl.hut: 155080 bytes
=> thurston.howell.hut: 141790 bytes
=> skipper.crew.hut: 119766 bytes
=> fileserver.copyroom.hut: 98926 bytes
=> gilligan.crew.hut: 78666 bytes
gilligan.crew.hut: 983860 bytes
=> laser3.copyroom.hut: 240327 bytes
=> maryann.girl.hut: 145361 bytes
=> skipper.crew.hut: 118827 bytes
=> lovey.howell.hut: 107152 bytes
=> professor.hut: 101219 bytes
=> thurston.howell.hut: 97398 bytes
=> fileserver.copyroom.hut: 90369 bytes
=> ginger.girl.hut: 83207 bytes
ginger.girl.hut: 968495 bytes
=> laser3.copyroom.hut: 179803 bytes
=> lovey.howell.hut: 170948 bytes
=> maryann.girl.hut: 152634 bytes
=> professor.hut: 110300 bytes
=> thurston.howell.hut: 105940 bytes
=> skipper.crew.hut: 103592 bytes
=> fileserver.copyroom.hut: 79551 bytes
=> gilligan.crew.hut: 65727 bytes
maryann.girl.hut: 846806 bytes
=> laser3.copyroom.hut: 161843 bytes
=> lovey.howell.hut: 145271 bytes
=> ginger.girl.hut: 126902 bytes
=> thurston.howell.hut: 123456 bytes
=> professor.hut: 109526 bytes
=> skipper.crew.hut: 75155 bytes
=> fileserver.copyroom.hut: 63807 bytes
=> gilligan.crew.hut: 40846 bytes
professor.hut: 821931 bytes
=> laser3.copyroom.hut: 172882 bytes
=> lovey.howell.hut: 123396 bytes
=> ginger.girl.hut: 113685 bytes
=> maryann.girl.hut: 94786 bytes
=> thurston.howell.hut: 94363 bytes
=> skipper.crew.hut: 86642 bytes
=> gilligan.crew.hut: 82825 bytes
=> fileserver.copyroom.hut: 53352 bytes
fileserver.copyroom.hut: 770174 bytes
=> lovey.howell.hut: 160986 bytes
=> professor.hut: 146975 bytes
=> ginger.girl.hut: 103478 bytes
=> maryann.girl.hut: 99261 bytes
=> skipper.crew.hut: 88261 bytes
=> thurston.howell.hut: 86084 bytes
=> gilligan.crew.hut: 85129 bytes
thurston.howell.hut: 619077 bytes
=> laser3.copyroom.hut: 129628 bytes
=> maryann.girl.hut: 105137 bytes
=> lovey.howell.hut: 82972 bytes
=> professor.hut: 78576 bytes
=> skipper.crew.hut: 66538 bytes
=> ginger.girl.hut: 58580 bytes
=> gilligan.crew.hut: 56658 bytes
=> fileserver.copyroom.hut: 40988 bytes
skipper.crew.hut: 481951 bytes
=> professor.hut: 108779 bytes
=> laser3.copyroom.hut: 89505 bytes
=> ginger.girl.hut: 77621 bytes
=> maryann.girl.hut: 71880 bytes
=> lovey.howell.hut: 43427 bytes
=> thurston.howell.hut: 41726 bytes
=> gilligan.crew.hut: 29142 bytes
=> fileserver.copyroom.hut: 19871 bytes
$
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
import sys
import re
total_bytes = {}
srcs = []
all = "all"
with open(sys.argv[1]) as f:
for line in f:
if re.search(r"^#", line):
continue
line = line.rstrip()
src, dst, bytes = line.split()
bytes = int(bytes)
if src in total_bytes:
tb = total_bytes[src]
tb[all] += bytes
if dst in tb:
tb[dst] += bytes
else:
tb[dst] = bytes
else:
srcs.append(src)
total_bytes[src] = {}
tb = total_bytes[src]
tb[dst] = bytes
tb[all] = bytes
srcs.sort(key=lambda x: -total_bytes[x][all])
for src in srcs:
tb = total_bytes[src]
print("{0}: {1} bytes".format(src, tb[all]))
dsts = list(tb.keys())
dsts.sort(key=lambda x: -tb[x])
for dst in dsts:
if dst == all:
continue
print(" => {0}: {1} bytes".format(dst, tb[dst]))
入出力結果(Terminal)
$ ./sample.py coconet.dat
lovey.howell.hut: 1139833 bytes
=> laser3.copyroom.hut: 212456 bytes
=> professor.hut: 175065 bytes
=> ginger.girl.hut: 158084 bytes
=> maryann.girl.hut: 155080 bytes
=> thurston.howell.hut: 141790 bytes
=> skipper.crew.hut: 119766 bytes
=> fileserver.copyroom.hut: 98926 bytes
=> gilligan.crew.hut: 78666 bytes
gilligan.crew.hut: 983860 bytes
=> laser3.copyroom.hut: 240327 bytes
=> maryann.girl.hut: 145361 bytes
=> skipper.crew.hut: 118827 bytes
=> lovey.howell.hut: 107152 bytes
=> professor.hut: 101219 bytes
=> thurston.howell.hut: 97398 bytes
=> fileserver.copyroom.hut: 90369 bytes
=> ginger.girl.hut: 83207 bytes
ginger.girl.hut: 968495 bytes
=> laser3.copyroom.hut: 179803 bytes
=> lovey.howell.hut: 170948 bytes
=> maryann.girl.hut: 152634 bytes
=> professor.hut: 110300 bytes
=> thurston.howell.hut: 105940 bytes
=> skipper.crew.hut: 103592 bytes
=> fileserver.copyroom.hut: 79551 bytes
=> gilligan.crew.hut: 65727 bytes
maryann.girl.hut: 846806 bytes
=> laser3.copyroom.hut: 161843 bytes
=> lovey.howell.hut: 145271 bytes
=> ginger.girl.hut: 126902 bytes
=> thurston.howell.hut: 123456 bytes
=> professor.hut: 109526 bytes
=> skipper.crew.hut: 75155 bytes
=> fileserver.copyroom.hut: 63807 bytes
=> gilligan.crew.hut: 40846 bytes
professor.hut: 821931 bytes
=> laser3.copyroom.hut: 172882 bytes
=> lovey.howell.hut: 123396 bytes
=> ginger.girl.hut: 113685 bytes
=> maryann.girl.hut: 94786 bytes
=> thurston.howell.hut: 94363 bytes
=> skipper.crew.hut: 86642 bytes
=> gilligan.crew.hut: 82825 bytes
=> fileserver.copyroom.hut: 53352 bytes
fileserver.copyroom.hut: 770174 bytes
=> lovey.howell.hut: 160986 bytes
=> professor.hut: 146975 bytes
=> ginger.girl.hut: 103478 bytes
=> maryann.girl.hut: 99261 bytes
=> skipper.crew.hut: 88261 bytes
=> thurston.howell.hut: 86084 bytes
=> gilligan.crew.hut: 85129 bytes
thurston.howell.hut: 619077 bytes
=> laser3.copyroom.hut: 129628 bytes
=> maryann.girl.hut: 105137 bytes
=> lovey.howell.hut: 82972 bytes
=> professor.hut: 78576 bytes
=> skipper.crew.hut: 66538 bytes
=> ginger.girl.hut: 58580 bytes
=> gilligan.crew.hut: 56658 bytes
=> fileserver.copyroom.hut: 40988 bytes
skipper.crew.hut: 481951 bytes
=> professor.hut: 108779 bytes
=> laser3.copyroom.hut: 89505 bytes
=> ginger.girl.hut: 77621 bytes
=> maryann.girl.hut: 71880 bytes
=> lovey.howell.hut: 43427 bytes
=> thurston.howell.hut: 41726 bytes
=> gilligan.crew.hut: 29142 bytes
=> fileserver.copyroom.hut: 19871 bytes
$
0 コメント:
コメントを投稿