2012年5月25日金曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の6章(ハッシュ), 6.6(練習問題)2を解いてみる。

2.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

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

my %words;
while(<>){
  chomp;
  last if $_ =~ /^\s*$/;
  $words{$_} += 1;
}
for(sort keys %words){
  print "$_: $words{$_}回\n";
}

入出力結果(Terminal)

$ perl sample.pl
fred
barney
fred
dino
silma
fred

barney: 1回
dino: 1回
fred: 3回
silma: 1回
$

0 コメント:

コメントを投稿