「Perl入門」rindexメソッドで後から検索し最初の文字列の位置を取得する

環境
Perl 5.32.1
Windows 10 home 64bit

書式
rindex (検索する文字列, 検索する文字列, 検索開始位置);
先頭の1文字目の位置は0です。
文字列の後から検索して引数に指定した文字列が出現した位置を返します。
検索開始位置を指定します
該当の文字がなかったときは-1を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#! /usr/bin/perl
use strict;
use warnings;
use utf8;
binmode STDIN, ':encoding(cp932)';
binmode STDOUT, ':encoding(cp932)';
binmode STDERR, ':encoding(cp932)';
my $cft = "テストテスト";
print rindex($cft,'テ',5)."\n";
print rindex($cft,'テ',4)."\n";
print rindex($cft,'テ',3)."\n";
print rindex($cft,'テ',2)."\n";
print rindex($cft,'テ',1)."\n";
print rindex($cft,'テ',0)."\n";
#! /usr/bin/perl use strict; use warnings; use utf8; binmode STDIN, ':encoding(cp932)'; binmode STDOUT, ':encoding(cp932)'; binmode STDERR, ':encoding(cp932)'; my $cft = "テストテスト"; print rindex($cft,'テ',5)."\n"; print rindex($cft,'テ',4)."\n"; print rindex($cft,'テ',3)."\n"; print rindex($cft,'テ',2)."\n"; print rindex($cft,'テ',1)."\n"; print rindex($cft,'テ',0)."\n";
#! /usr/bin/perl
use strict;
use warnings;
use utf8;
binmode STDIN, ':encoding(cp932)';
binmode STDOUT, ':encoding(cp932)';
binmode STDERR, ':encoding(cp932)';

my $cft = "テストテスト";
print rindex($cft,'テ',5)."\n"; 
print rindex($cft,'テ',4)."\n"; 
print rindex($cft,'テ',3)."\n"; 
print rindex($cft,'テ',2)."\n";
print rindex($cft,'テ',1)."\n";
print rindex($cft,'テ',0)."\n";

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\study\skill\perl>perl 11.pl
3
3
3
0
0
0
C:\study\skill\perl>perl 11.pl 3 3 3 0 0 0
C:\study\skill\perl>perl 11.pl
3
3
3
0
0
0

 

Perl

Posted by arkgame