我正在尝试读取一个看起来像这样的文件:
> SOME HEADER
ABCDEFGHIJKLMNOP
QRSTUPWXYZ123456
我需要能够读取字符A,B,C,D等。字符,所以我正在使用这个循环:
while (read $file, my $char, 1){
print $char;
print $.;
print "\n\n";
}
但问题是,我需要跳过“SOME HEADER”。我需要跳过它不是基于它是第一行,而是基于具有“> SOME HEADER”子串。
打印$。即使它不是正确的行号,也总是输出“6”。
答案 0 :(得分:0)
open my $file,"<","file.txt";
<$file>; #Removing first line
while (read $file, my $char, 1)
{
print $char;
}
或设置标志并检查
my $flag = 0;
while (read $file, my $char, 1)
{
$flag = 1 and next if(($char =~m/\n|\r\n/) && ($flag == 0));
next if $flag == 0;
print $char;
}
答案 1 :(得分:0)
逐行处理和然后逐个字符处理会不会更容易?
use strict;
use warnings;
while (<DATA>) {
chomp;
next if /^> SOME HEADER/;
for my $char (split(//)) {
print "$char $.\n";
}
}
__DATA__
> SOME HEADER
ABCDEFGHIJKLMNOP
QRSTUPWXYZ123456
输出:
A 2
B 2
C 2
...
4 3
5 3
6 3