Perl从另一个集合打印集合
我有一个脚本,它几乎只是从垂直到水平打印数据集 我是想跳过一些我不想要的名字。 但是我想打印出来,只需选择首字母,同时保留订单Perl从另一个集合打印集合,perl,Perl,我有一个脚本,它几乎只是从垂直到水平打印数据集 我是想跳过一些我不想要的名字。 但是我想打印出来,只需选择首字母,同时保留订单 #!/usr/bin/perl use strict; use warnings; use Data::Dumper ; my $filename = shift @ARGV ; open(my $fh, '<', $filename) or die "Could not open file $filename $!"; while (<$fh>)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper ;
my $filename = shift @ARGV ;
open(my $fh, '<', $filename) or die "Could not open file $filename $!";
while (<$fh>) {
next if /^$/;
chomp $_ ;
print $_ ;
}
所以从这一组中——
LC:
NW:
DS:
AP:
II:
NW:
KB:
JK:
LC:
DS:
TM:
AP:
WJ:
这些不会被打印到终端
LC:
NW:
II:
NW:
KB:
JK:
TM:
这些将被打印到终端-水平,保留订单
DS: AP: AP: WJ:
如果我们不想硬编码列表呢 准备: 循环中:
next if !/$to_keep_re/;
next if !$to_keep{$_};
一种不需要对列表进行硬编码的更快的替代方法 准备: 循环中:
next if !/$to_keep_re/;
next if !$to_keep{$_};
如果/(?:WJ | DS | AP):/被否定,您可以尝试
打印。固定的。
my @to_keep = qw( AP DS WJ );
my %to_keep = map { "$_:" => 1 } @to_keep;
next if !$to_keep{$_};