開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の15章(スマートマッチとgiven-when), 15.6(練習問題)2を解いてみる。
2.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
sub fizz_bin_sausage{
my $num = shift;
my $result = "";
given($num){
when($_ % 3 == 0){ $result .= "Fizz ";continue;}
when($_ % 5 == 0){ $result .= "Bin "; continue;}
when($_ % 7 == 0){ $result .= "Sausage";}
}
return $result;
}
my $i = 1;
while(1){
my $result = fizz_bin_sausage $i;
say "$i: $result";
last if $result eq "Fizz Bin Sausage";
$i++;
}
入出力結果(Terminal)
$ perl perl_program 1: 2: 3: Fizz 4: 5: Bin 6: Fizz 7: Sausage 8: 9: Fizz 10: Bin 11: 12: Fizz 13: 14: Sausage 15: Fizz Bin 16: 17: 18: Fizz 19: 20: Bin 21: Fizz Sausage 22: 23: 24: Fizz 25: Bin 26: 27: Fizz 28: Sausage 29: 30: Fizz Bin 31: 32: 33: Fizz 34: 35: Bin Sausage 36: Fizz 37: 38: 39: Fizz 40: Bin 41: 42: Fizz Sausage 43: 44: 45: Fizz Bin 46: 47: 48: Fizz 49: Sausage 50: Bin 51: Fizz 52: 53: 54: Fizz 55: Bin 56: Sausage 57: Fizz 58: 59: 60: Fizz Bin 61: 62: 63: Fizz Sausage 64: 65: Bin 66: Fizz 67: 68: 69: Fizz 70: Bin Sausage 71: 72: Fizz 73: 74: 75: Fizz Bin 76: 77: Sausage 78: Fizz 79: 80: Bin 81: Fizz 82: 83: 84: Fizz Sausage 85: Bin 86: 87: Fizz 88: 89: 90: Fizz Bin 91: Sausage 92: 93: Fizz 94: 95: Bin 96: Fizz 97: 98: Sausage 99: Fizz 100: Bin 101: 102: Fizz 103: 104: 105: Fizz Bin Sausage $
0 コメント:
コメントを投稿