開発環境
- 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) の14章(文字列処理とソート), 14.5(練習問題)1を解いてみる。
1.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl
use strict;
use warnings;
my @list = ();
while(<>){
push @list,split;
}
my @sorted = sort {$a <=> $b} @list;
my $width = 0;
for(@sorted){
$width = length $_ if length $_ > $width;
}
for(@sorted){
printf "%${width}g\n",$_;
}
入出力結果(Terminal)
$ ./sample.pl numbers
-40
-10
0
0
1
1.5
1.5
2
2.71828
3.14159
4
4
9
17
42
98.6
666
1000
2001
2001
90210
$ cat numbers
17 1000 04 1.50 3.14159
-10 1.5 4 2001 90210 666
9 0 2 1 0
2001 42 -40 98.6 2.71828
$
0 コメント:
コメントを投稿