Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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.

CODE
$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"; } }
HERE IS THE DATA
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

Comment on don't want to print certain lines
Select or Download Code
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.

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"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402872]
Approved by atcroft
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-10-22 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls