2013年2月4日月曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の8章(リファレンスを使った実践的なテクニック), 9.9(練習問題)2を解いてみる。

その他参考書籍

3.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use Benchmark qw(:all);

my @words = qw(e a d b c E A D B C);

my @sorted = map {$_->[0]} sort {$a->[1] cmp $b->[1]} map {
    my $string = $_;
    $string =~ tr/A-Z/a-z/;
    $string =~ tr/a-z//cd;
    [$_, $string]
    } @words;

print map {"$_\n"} @sorted;

入出力結果(Terminal)

$ ./sample.pl
a
A
b
B
c
C
d
D
e
E
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var words = ['e','a','d','b','c','E','A','D','B','C'];
words.sort( function( a, b ) {
    a = (a).toString().toUpperCase();
    b = (b).toString().toUpperCase();
    if (a < b){
        return -1;
    } else if(a > b){
        return 1;
    } else {
        return 0;
    }
});
$('#pre0').text(words);



pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

words = ['e','a','d','b','c','E','A','D','B','C']
words.sort(key=lambda x: str(x).upper())

print(words)

入出力結果(Terminal)

$ ./sample.py
['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E']
$

0 コメント:

コメントを投稿