in reply to remove a blank line after a string

Adding one line will do it. I'll demonstrate:

my $msg = $pop->get($msgnum); pop @{$msg} if $msg->[-1] =~ /^\s+$/; # This is the additional line. print MAILBOX @$msg;

That assumes that your description of the problem is correct; that there is one blank line at the end of the message, with nothing but whitespace in it (whitespace includes \n). This will essentially eliminate that last line if it only contains \s character(s).


Dave

Replies are listed 'Best First'.
Re^2: remove a blank line after a string
by johnajb (Novice) on Jan 26, 2005 at 18:05 UTC
    Thanks for the reply. The blank line is actually in the middle of the message following the content type 7bit line. What might work is to get rid of all blank lines

      Ok, I misunderstood. This should do the trick:

      for ( 0 .. $#{$msg} ) { next unless $msg->[$_] =~ m/\QContent-Transfer-Encoding: 7bit\E/i; splice( @{$msg}, $_ + 1, 1 ) if $msg->[ $_ + 1 ] =~ m/^\s+$/; last; }

      This scans through the message until it finds the "Content-Transfer-Encoding: 7bit" string. The next line is then checked to see if it is blank except for whitespace, and if it is, it's deleted. And since we're done looking for the blank line, the loop is exited.


      Dave

        A range operator would give you an arguably elegant rewrite:
        @$msg = grep {(m/\QContent-Transfer-Encoding: 7bit\E/i .. /^\s*$/) ne +'2E0'} @$msg;
        or, to use the splice and short-circuiting:
        my $i = 0; for (@$msg) { if ((/\QContent-Transfer-Encoding: 7bit\E/ .. /^\s*$/) eq '2E0') { splice(@$msg, $i, 1); last; } ++$i; }

        Caution: Contents may have been coded under pressure.
        You are my holy father!!!!!!!!!!!!!!!!

        Seems a bit complex for filtering an array, yeah? Will something this work? (Can't test it myself right now.)

        my @msg = grep /\S/, @{$pop->get($msgnum)}; # then the @{$msg} stuff has to change, of course

        How would i write the lines prior to the deleted blank line to a file?

        my $msg = $pop->get($msgnum); for ( 0 .. $#{$msg} ) { next unless $msg->[$_] =~ m/\QContent-Transfer-Encoding: 7bit\E/i; splice( @{$msg}, $_ + 1, 1 ) if $msg->[ $_ + 1 ] =~ m/^\s+$/; delete last; }

        Code tags and formatting added by davido. Please read Writeup Formatting Tips.