開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2)の6章(ハッシュ)の6.6(練習問題)2を解いてみる。
その他参考書籍
2.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
my %names = (fred => 'flintstone',
barney => 'rubble',
wilma => 'flintstone');
while (1) {
print "名前を入力: ";
chomp(my $first_name = <STDIN>);
last if $first_name =~ /^\s*$/;
print "$names{$first_name}\n";
}
入出力結果(Terminal)
$ ./sample.pl barney: 1回 dino: 1回 fred: 3回 wilma: 1回 $ cat sample.txt fred barney fred dino wilma fred $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
count = {}
words = []
with open("sample.txt") as f:
for word in f.readlines():
word = word.rstrip()
if word in count:
count[word] += 1
else:
words.append(word)
count[word] = 1
for word in sorted(words):
print("{0}: {1}回".format(word, count[word]))
入出力結果(Terminal)
$ ./sample.py barney: 1回 dino: 1回 fred: 3回 wilma: 1回 $
0 コメント:
コメントを投稿