Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Delete lines

by Anonymous Monk
on Aug 16, 2009 at 15:04 UTC ( #789021=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to delete the current line with the string '{T}' and next line if there is a single character in the next line. The below code deletes all the single characters in a line if exists. I want to delete only the single string if below {T}.
#!/usr/bin/perl while (<DATA>) { chomp; s/^[A-Z]$//g; $_ = '' if index( $_, '{T}' ) >= 0; print $_; } __DATA__ 020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} R Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} R G8 Gardening< January garden calendar B C

Comment on Delete lines
Download Code
Re: Delete lines
by linuxer (Deacon) on Aug 16, 2009 at 15:43 UTC

    What about doing it this way.

    Currently it skips any line containing the string "{T}". And if the following line consists of a single alphanumeric, that line is skipped as well.

    #! /usr/bin/perl use strict; use warnings; my $flag_match; LINE: while ( my $line = <DATA> ) { if ( $line =~ m/{T}/ ) { $flag_match = 1; next LINE; } else { if ( $line =~ m/^\w$/ && $flag_match ) { $flag_match = 0; next LINE; } } print $line; } __DATA__ 020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} R Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} R G8 Gardening< January garden calendar B C
Re: Delete lines
by ssandv (Hermit) on Aug 16, 2009 at 16:10 UTC

    Because you're simply reading in lines and then printing them out again, I suggest that rather than change what's in $_ you use your conditional construct (if {...} else {...}) to decide what actions to actually take with each line. Then it's a simple matter to discard another line:

    if (/\{T\}/) { my $throwaway_line=<DATA>; # that line will never get printed now } else { print ; # print with no arguments prints $_ }

    Basically if you just read another line from the file, and don't print it, you'll get what you want, I think. (I'm having a little trouble reading your post, so my code may not quite solve the problem you're describing, but hopefully it's close enough that you can adapt it.)

    I also highly recommend using strict and warnings as standard practice.

Re: Delete lines
by jwkrahn (Monsignor) on Aug 16, 2009 at 16:14 UTC
    #!/usr/bin/perl use warnings; use strict; while ( <DATA> ) { next if /{T}/ and $_ .= <DATA> and /^.\Z/m; print; } __DATA__ 020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} R Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} R G8 Gardening< January garden calendar B C
Re: Delete lines
by GrandFather (Cardinal) on Aug 16, 2009 at 20:42 UTC

    The code does more than just delete lines containing only a single uppercase letter, it also concatenates everything that remains into a single line. Is that the intent?

    You actually ask how to do two contradictory things. Your first request implies deleting the line containing {T} and the following line. The last request asks that only a line containing a single letter be deleted if it follows a line containing {T}. Nowhere do you indicate if it is important that the {T} be at the end of the line.

    use warnings; use strict; while (<DATA>) { no warnings "void"; m/{T}$/ .. s/^[A-Z]\n//; print; } __DATA__ 020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} R Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} R G8 Gardening< January garden calendar B C

    Prints:

    020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} G8 Gardening< January garden calendar B C

    Note the use of no warnings "void" to suppress the 'useless use ...' warning due to using the range operator for a side effect.

    Minor alterations of the match (possibly to a substitution) can provide the other variants implied by your question.


    True laziness is hard work
Re: Delete lines
by bichonfrise74 (Vicar) on Aug 17, 2009 at 01:13 UTC
    Here's to get you started...
    #!/usr/bin/perl use strict; no warnings; my @lines = <DATA>; for my $i (0 .. $#lines) { if ( $lines[$i] =~ /\{T\}/ && $lines[$i + 1] =~ /^\w$/ ) { splice( @lines, ($i + 1 ), 1 ); } } print @lines; __DATA__ 020209990421VUTVS01 00082445B3^V^V^A2^C^D^V^V^A os00 0001 000101 R S 9 +912290449 00005941^B{T} R Saturday, January 1, 2000 G1 G ^C^D^V^V^A os00 0002 000101 R S 9912290451 00004113^B{T} R G8 Gardening< January garden calendar B C
Re: Delete lines
by sanku (Beadle) on Aug 25, 2009 at 09:19 UTC
    I think this is the thing you need.
    open(DATA,"ss.txt") or die $!; @data=<DATA>; close(DATA); foreach $i(0 .. scalar @data){ if($data[$i] =~/\{T\}/){ $data[$i]=~s/$data[$i]//g; $data[$i+1]=~s/$data[$i+1]//g; } else{ print "$data[$i]"; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-09-17 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (88 votes), past polls