正确使用~~

问题描述 投票:1回答:1

我试图解析一个简单的字段和值对文件。所以我不感兴趣的一些领域,我想跳过

所以在我的“播放”代码中,我有一个像这样的静态事物:next if if field = ~m / fieldToIgnore1 | fieldToIgnore2 /;

...然后我扩展了一个阵列,仍然很高兴

print "== using ~~ ==\n";
foreach my $field (@fields) {
  next if $field ~~ @foni;
  print "$field\n";
}

(fnoi ==不感兴趣的字段)

但当我把它带回我的非播放设置时,它不起作用。现在在戏剧中我只是在循环

my @fields = ("field1", "field2");
my @foni = ("fieldToIgnore1", "fieldToIgnore1");

在我正确的代码中,我遍历每一行并取出设置为字段 - 值行的行,然后将字段删除为标量...因此我认为它与我的播放代码的想法相同 - 但它似乎不是

while ( <$infile> ) {

  if ( /^PUBLISH:/ ) {

    ( $symbol, $record_type ) = ( $1, $2 );
    print "symbol is: [$symbol]\n"; 

  } else {
    my ( $field, $value ) = split(/\|/);
    next unless $value;

    print "field is: [$field]\n";
    print "value is: [$value]\n";

    $field =~ s/^\s+|\s+$//g;
    $value =~ s/^\s+|\s+$//g;

    print "... field is: [$field]\n";
    print "... value is: [$value]\n";

    ## ADD FIELD SKIPPING LOGIC HERE  
regex perl
1个回答
0
投票

你可以从你的数组中构建一个正则表达式模式,就像这样

my $re = join '|', @foni;
$re    = qr/$re/;                  # Compile the regex

for my $field (@fields) {
    next if $field =~ $re;
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.