sub get_info () {
open(FILE,"file.txt") or die "Could not open file";
my @list = grep /\bWarning\b/, <FILE>;
$count = 0;
chomp @list;
$list[$count] =~ s/^\S+\s*//;
#do something;
$count++;
}
file.txt -
Warning: New layer 256 is added
Warning: Layer deleted
Warning: New layer 301 is added
我想忽略警告&#39;从每一行开始。
但它隐藏着“警告”这个词。仅从第一句开始。
我是否正确递增$ count?
答案 0 :(得分:2)
这是因为您只对mockMvc
进行替换。因此,只有第一个索引被修改。
fh = os.popen(bash_com,'r')
data = fh.read()
newf = open("/var/tmp/t1.txt",'w')
sys.stdout = newf
print data
with open("/var/tmp/t1.txt") as json_data:
j = json.load(json_data)
print j['id']
您没有使用任何循环,因此$count=0
没有意义。
<强>解决方案:强>
$list[$count] =~ s/^\S+\s*//g;
<强>输出:强>
count
改进:使用3个args打开,删除grep和搜索替换,添加while循环逐行读取文件
#!/usr/bin/perl
use strict;
use warnings;
sub get_info{
open(FILE,"file.txt") or die "Could not open file";
my @list = grep /\bWarning\b/, <FILE>;
my $count = 0;
foreach (@list) {
$list[$count] =~ s/^\S+\s*//g;
$count++;
}
print @list;
#do something;
}
get_info();