我有一串像
这样的行comments:[I#1278327] is related to office communicator.i fixed the bug to declare it null at first time.
我在这里搜索I#
的索引,然后我希望整个单词意味着[I#1278327]
。我是这样做的:
open(READ1,"<letter.txt");
while(<READ1>)
{
if(index($_,"I#")!=-1)
{
$indexof=index($_,"I#");
print $indexof,"\n";
$string=substr($_,$indexof);##i m cutting that string first from index of I# to end then...
$string=substr($string,0,index($string," "));
$lengthof=length($string);
print $lengthof,"\n";
print $string,"\n";
print $_,"\n";
}
}
perl中是否有任何API在查找该行中的I#
索引后直接找到字长。
答案 0 :(得分:3)
您可以执行以下操作:
$indexof=index($_,"I#");
$index2 = index($_,' ',$indexof);
$lengthof = $index2 - $indexof;
然而,更大的问题是你使用Perl就好像它是BASIC一样。打印选定行的任务更加美观:
use strict;
use warnings;
open my $read, '<', 'letter.txt'; # safer version of open
LINE:
while (<$read>) {
print "$1 - $_" if (/(I#.*?) /);
}
答案 1 :(得分:1)