in reply to Re^2: Ignoring lines in flat text file
in thread Ignoring lines in flat text file

The entire expression must be in parens.

You also have logic errors. Your expression will always be true.

if ( $line !~ m/^2/ || ( substr($line, 14, 3) ne '800' && substr($line, 14, 3) ne '866' && substr($line, 14, 3) ne '877' && substr($line, 14, 3) ne '888' ) ) { ... }

That could be simplified using a regexp or a lookup table

if ( $line !~ m/^2/ || substr($line, 14, 3) !~ /^(?:800|866|877|888)\z/ ) { ... }
my %tollfree = map { $_ => 1 } 800, 866, 877, 888; if ( $line !~ m/^2/ || !$tollfree{ substr($line, 14, 3) } ) { ... }

Replies are listed 'Best First'.
Re^4: Ignoring lines in flat text file
by sheasbys (Initiate) on Sep 11, 2007 at 16:47 UTC
    When I use this code, without your suggestions, I preserve the contents of any line starting with a "2," which is desirable:

    if ( $line !~ m/^2/ ) { . . . else { print OUTPUT $line . "\n";

    My goal is to preserve any line starting with a 2 and write it to output as well as writing any line where positions 14-16 are not 800, 866, 877 or 888. While the logic appears sound to me it does not make sense why these lines are not written to the output.

    BTW I really appreciate your help.

      I guess I understood a little backwards earlier.

      my %tollfree = map { $_ => 1 } 800, 866, 877, 888; if ( $line =~ m/^2/ || !$tollfree{ substr($line, 14, 3) } ) { print OUTPUT "$line\n"; } else { ... }
      A reply falls below the community's threshold of quality. You may see it by logging in.