2013年7月15日月曜日

開発環境

『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2)の5章(入出力)の5.13(練習問題)3を解いてみる。

その他参考書籍

3.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

print "カラムの幅を指定: ";
chomp(my $cols = <STDIN>);
print "文字列のリストを一行に一個ずつ入力\n";
chomp( my @strs = <STDIN> );

print 1234567890 x ($cols / 10 + 1), "\n";

for (@strs) {
    printf "%${cols}s\n", $_;
}

入出力結果(Terminal)

$ ./sample.pl
カラムの幅を指定: 30
文字列のリストを一行に一個ずつ入力
hello
good-bye
1234567890123456789012345678901234567890
                         hello
                      good-bye
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

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

strs = []

cols = int(input("カラムの幅を指定: "))
print("文字列のリストを一行に一個ずつ入力")
while True:
    s = input()
    if s == "":
        break
    strs.append(s)

print("1234567890" * (cols // 10 + 1))

for s in strs:
    print(s.rjust(cols))

入出力結果(Terminal)

$ ./sample.py
カラムの幅を指定: 30
文字列のリストを一行に一個ずつ入力
spame
egg

1234567890123456789012345678901234567890
                         spame
                           egg
$

0 コメント:

コメントを投稿