in reply to Find a value from an array in a string

Three notes:
  1. Did you remember that arrays are zero-based? Is it $field[8] you want or $field[7]?
  2. You didn't chomp() your input line, so the last field will have a newline at the end. Hey! That's my bug!
  3. You would do better with a hash for the accounts
my %skipaccount; $skipaccount{$_} = 1 for qw(77403 77404 77406 77407 77408 77409 77411 +77412 77413 77414 77416 77418); later: $suppress = 1 if $skipaccount{$fields[8]};

Replies are listed 'Best First'.
Re: Re: Find a value from an array in a string
by Skyler99 (Novice) on Mar 13, 2003 at 20:32 UTC
    Dear Monk, I appreciate your help. I owe you a million. Could you tell me if there may be any other way that I could match the array number to the number located in Sequence 8 on the FT1 line.?? Thanks in advance.
    while (<IN>) { chomp(); if (/^(MSH)(.....)/) { setdelimiters($2); } @field = split /$fs/; if ($newblock{$field[0]}) { processbuffer(); } elsif (!$partofblock{$field[0]}) { print STDERR "Possible error: segment=$field[0] line $.\n"; } if ($field[0] eq "FT1") { if ($field[7] == $skipaccount{$_}) { $suppress = 1 if $skipaccount{$field[7]}; } } $buffer .= "$_\n"; } processbuffer(); sub processbuffer { print OUT $buffer unless $suppress; $buffer=""; $suppress = 0; }
    MSH|^~\&||||||||||||| EVN||||| PID||||||||||||||||||||| FT1|||||||77413| MEV|||||||||||||||||||
      Instead of:
      if ($field[0] eq "FT1") { if ($field[7] == $skipaccount{$_}) { $suppress = 1 if $skipaccount{$field[7]}; } }
      you just need:
      if ($field[0] eq "FT1") { $suppress = 1 if $skipaccount{$field[7]}; }
      Of course there are a lot of ways to do it--TMTOWTDI