我正在尝试在 Perl 中创建一个程序,该程序将读取数千个字符并尝试找到匹配的字符串。我需要打印出字符串加上接下来的五个字母。我还需要打印出找到它的位置,即有多少个字母。我对 Perl 很陌生。我现在只是在课堂上学习它。
这是我到目前为止的代码:
#!/usr/bin/perl
$sequence = 'abcd';
$fileName = 'file.txt';
#Opening file
unless (open(fileName, $fileName)) {
print "Cannot open file.";
exit;
}
@tempArr = <fileName>; #Adding the lines to an array
close fileName; #closing the file
$characters = join('', @tempArr); #making it a clean string
$characters =~ s/\s//g; #removing white lines
if (characters =~ m/$sequence/i) {
#Print $sequence and next five characters
}
else {
print "Does not contain the sequence.";
}
exit;
file.txt 看起来像:
aajbkjklasjlksjadlasjdaljasdlkajs
aabasdajlakjdlasdjkalsdkjalsdkjds
askdjakldamwnemwnamsndjawekljadsa
abcassdadadfaasabsadfabcdhereeakj
我需要打印出“abcdheree”