2012年10月2日火曜日

開発環境

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

その他参考書籍

3.

コード(TextWrangler)

sample.pl

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

print "文字列を入力: ";
chomp(my $str = <STDIN>);
while(1){
  print "部分文字列を入力: ";
  chomp(my $substr = <STDIN>);
  last if $substr =~ /^\s*$/;
  my $where = -1;
  while(1){
    $where = index($str, $substr, $where + 1);
    last if $where == -1;
    print "$where ";
  }
  print "\n";
}

入出力結果(Terminal)

$ ./sample.pl
文字列を入力: This is a test
部分文字列を入力: is
2 5 
部分文字列を入力: a
8 
部分文字列を入力: t
10 13 
部分文字列を入力: 
$

0 コメント:

コメントを投稿