Perlメモ

本から抜粋・・・^^;

データ配列から目標文字列「ACCC」を検索し、各マッチの開始位置を記憶。


[root@ns tmp 20:14:45]# vi ./test.pl#!/usr/bin/perl -w
my $target = "ACCC";
my $search_string =
'CCACACCACACCCACACACCCACACACACACA'.
'CATCCTAACACTACCTAACACAGCCTAACTAA'.
'ACCCTCCATTACCTHCCTCCACTCHTTACCTG';

my @macthes;

foreach my $i (0..length $search_string){
if( $target eq substr( $search_string, $i, length $target)){
push @matches, $i;
}
}

print "My matches occuered at the follwing offsets: @matches.\n";
print "done\n";
[root@ns tmp 20:14:58]#

実行結果


[root@ns tmp 20:14:59]# ./test.pl
My matches occuered at the follwing offsets: 9 17 64.
done
[root@ns tmp 20:17:17]#