|
楼主 |
发表于 2005-7-25 11:59:18
|
显示全部楼层
遍历搜索字符串
从左向右
[PHP]
#!/usr/bin/perl
$a="aa ab ac abc ab";
sub searchstr {
return index ($a, $char,$search);
}
print "Input search sub string:";
$char=<STDIN>;
chomp $char;
print "Search start:";
$search=<STDIN>;
chomp $search;
while (($search=searchstr($a,$char,$search)) != -1) {
print " ",searchstr($a,$char,$search);
$search=$search+1;
}
[/PHP]
从右向左
[PHP]
#!/usr/bin/perl
$a="aa ab ac abc ab";
$search=length($a);
sub searchstr {
return rindex ($a, $char,$search);
}
print "Input search sub string:";
$char=<STDIN>;
chomp $char;
while (($search=searchstr($a,$char,$search)) != -1) {
print " ",searchstr($a,$char,$search);
$search=$search-1;
}
[/PHP] |
|