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

Thanks for the advice. I have changed my strategy to have the look up ignore the line as is as along as it does not match the 4 options listed in the code below but I get a syntax error. As I am still a newbie I am not sure what is wrong with this snippet of code:

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


Thanks again.

Replies are listed 'Best First'.
Re^3: Ignoring lines in flat text file
by ikegami (Patriarch) on Sep 11, 2007 at 15:28 UTC

    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) } ) { ... }
      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.