Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

don't want to print certain lines

by dee00zee (Novice)
on Oct 27, 2004 at 01:20 UTC ( #402872=perlquestion: print w/replies, xml ) Need Help??
dee00zee has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I want to print only the lines other than FIELD
and ); following it on the next line, but my code didn't work.

$ver="abc.txt"; open (IN,$ver); $new="new.txt"; open(OUT,"> $new"); while ($string=<IN>) { if ($string=~ /^FIELD/) { next; $line=<IN>; next if $line=<IN>; } else { @lines=split(/\s+/,$string); print OUT "@lines\n"; } }
These are all wanted data ); Please insert your marks ); FIELD 10 ); FIELD 11 ); FIELD 12 ); FIELD 13 ); FIELD 14 );

Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: don't want to print certain lines
by bart (Canon) on Oct 27, 2004 at 01:56 UTC
    Your basic problem is you're trying to do more stuff under a statement consisting only of next, the code underneath it won't ever be executed.

    Second, you seem to be testing whether a line contains nothing but ");" with a bare if. Newsflash: ");" as a string has a true value. You need a better test. And "=" is not the proper comparison operator.

    The following code would appear to do what you want:

    while ($string=<IN>) { if ($string=~ /^FIELD/) { while($line=<IN>) { last if $line =~ /^\);$/; } } else { @lines=split(/\s+/,$string); print "@lines\n"; } }

    Something else to reconsider: Apparently you treat ");" on a line of its own as special. Perhaps you should reconsider setting $/ to it, treating it as a "line" terminator? That might make things easier.

    local $/ = ");\n"; local $\ = $/; while ($string=<IN>) { if ($string=~ /^FIELD/) { # no need to skip anything, everything is in $string } else { chomp $string; # get rid of trailing ");" # print will add it again, thanks to $\ @lines=split(/\s+/,$string); print "@lines\n"; } }
Re: don't want to print certain lines
by krusty (Hermit) on Oct 27, 2004 at 01:37 UTC
    Try the following:
    $ver="abc.txt"; open (IN,$ver); $new="new.txt"; open(OUT,"> $new"); while (<IN>){ <IN> and next if /^FIELD/; print OUT; }

    This should print to new.txt all lines except any beginning with FIELD and the line immediately following.
      I try it and it works..thank you.
      But I don't understand this part

      <IN> and next if /^FIELD/;
      print OUT;
      What does <IN> do here and why it didn't print ); that
      appear after FIELD lines:?

        The problem is your first next in your if block, any thing in that block after that next will never be executed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402872]
Approved by atcroft
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-06-21 07:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.