開発環境
- 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 @numbers = ();
my $length = 0;
while(<>){
push @numbers, split;
}
for(@numbers){
$length = length $_ if $length < length $_;
}
for(sort{$a <=> $b} @numbers){
printf "%${length}s\n",$_;
}
入出力結果(Terminal)
$ 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
$ ./perl_program numbers
-40
-10
0
0
1
1.50
1.5
2
2.71828
3.14159
04
4
9
17
42
98.6
666
1000
2001
2001
90210
$
0 コメント:
コメントを投稿