例如,在一个perl文件中,它是这样的:
123 abc xyz
456 abc sdf
789 abc ghj
现在,从以上几行中,我需要将删除abc
和分配分开
value 1=123
value 2= xyz
value 3= 456
value 4= sdf
帮助我如何为上述要求编码。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my $count = 0;
while (<DATA>) {
chomp;
for (split / abc /) {
$count++;
say "value $count=", ($count == 1 ? '' : ' '), $_;
}
}
__DATA__
123 abc xyz
456 abc sdf
789 abc ghj
或者,也许是一种更数据驱动的方法来确定哪些行在=
:之后省略了空格:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my %nospace = (
1 => 1,
);
my $count = 0;
while (<DATA>) {
chomp;
for (split / abc /) {
$count++;
say "value $count=", ($nospace{$count} ? '' : ' '), $_;
}
}
__DATA__
123 abc xyz
456 abc sdf
789 abc ghj
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句