sashac88 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, saint gurus.
I use flip flop operator to select/deselect certain lines in big text file ,which is output from device's log.
Somehow certain lines fit ,while other lines with the same content don't.
Is flip flop operator reliable? Please look at my code and suggest corrections.Also any performance improvement suggestions are very appreciated.
my $backup=$commandfile.".bak"; foreach $com (keys %hashnew) { $ARGV[0]="$backup"; $^I=".bak"; while (<>) { if ($hashnew{$com} == "all") { unless (/$hashnew{$com}/../\*\ Exit\ context\ \*/) { s/$com//g; } } if (/$hashnew{$com}/../\*\ Exit\ context\ \*/) { s/$com//g; } print; } }

Replies are listed 'Best First'.
Re: Flip flop operator
by Zaxo (Archbishop) on Aug 19, 2004 at 07:45 UTC

    You exhaust <> in your first pass. Unfortunately, you can't seek in tty STDIN, so you'll need to invert your logic, making all your comparisons in a single pass of <>. I don't think your range operators will take the same form in that rewrite.

    If you mean to iterate over a seekable handle, you'll still probably be better off to only read once.

    After Compline,
    Zaxo

      I have the feeling you missed the $ARGV[0]= line.
      I don't have much expirience with PERL.
      I need to take off specific lines from some file.
      How should I do it?
      Thanks in advance for the answer!

        It is probably simpler to just slurp the entire file into an array and replace the while(<>) with an iteration over the array (untested code follows):

        my @data = <>; my $backup=$commandfile.".bak"; foreach $com (keys %hashnew) { $ARGV[0]="$backup"; $^I=".bak"; foreach (@data) { if ($hashnew{$com} == "all") { unless (/$hashnew{$com}/../\*\ Exit\ context\ \*/) { s/$com//g; } } if (/$hashnew{$com}/../\*\ Exit\ context\ \*/) { s/$com//g; } print; } }
        Of course if you actually explained to us what you want to do we might be able to come up with a better suggestion as it is not entirely clear from your code.

        /J\