in reply to Re^2: remove a blank line after a string
in thread remove a blank line after a string

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

Replies are listed 'Best First'.
Re^4: remove a blank line after a string
by Roy Johnson (Monsignor) on Jan 27, 2005 at 03:40 UTC
    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.
Re^4: remove a blank line after a string
by johnajb (Novice) on Jan 26, 2005 at 18:27 UTC
    You are my holy father!!!!!!!!!!!!!!!!
Re^4: remove a blank line after a string
by Your Mother (Archbishop) on Jan 27, 2005 at 00:28 UTC

    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

      That would remove all blank lines, which may be ok with the OP, but doesn't sound all that desirable to me. I would think that one would want to keep the message content intact.

      Perhaps it's complex, but my solution does only remove one blank line, and that line can only follow the "Content-type" line. I'll admit the range operator would have been more elegant though.


      Dave

Re^4: remove a blank line after a string
by johnajb (Novice) on Feb 19, 2005 at 01:06 UTC

    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.

      Maybe like this? (I'm sure you could come up with something yourself if you thought about it)

      my $msg = $pop->get($msgnum); LOOP: for ( 0 .. $#{$msg} ) { unless( $msg->[$_] =~ m/\QContent-Transfer-Encoding: 7bit\E/i ) { print OUTFILE $msg->[$_]; next LOOP; } splice( @{$msg}, $_ + 1, 1 ) if $msg->[ $_ + 1 ] =~ m/^\s+$/; # delete # This is probably a mistake. Delete what? And where's +the semicolon? last LOOP; }

      Dave


        Hi All,
        I really need your help on this one. I must apologize that my formatting has been terrible. I tried to use the code tag but it
        I need to we-write an email header and none of the mail modules work well for what want to do because the mail is non-compliant. I need to find the first instance of the Recipients field. For some reason it is not in the header, it is in the body.
        I need to write the recipients field and associated email to a new file. (No whitespace allowed) I then need to move down to the Content-Transfer-Encoding: 7bit field (the first instance as well), and append the rest of the mail (after the blank space after the Content-Transfer-Encoding: 7bit field) to the new file under the last recipient email address with no blank lines. This will make the emails compliant so I can properly parse them
        It may be easier just to remove the blank field after Content-Transfer-Encoding: 7bit;, then remove Content-Transfer-Encoding: 7bit; and all lines above it besides the recipients field and emails. (There are a couple of blank lines above and below the recipients and emails). I am not sure and would appreciate some help. I have included the recipients field and the lines above it below. There is nothing but blank space under the recipients,
        Thanks
        Sender: "Capturil" <smtp:Capturil@manywhere.com> Message-ID: <166E9D4D3E50EF4E87B2C7AC28634094784B@REDSERV1.RM.local> Recipients: "warren glick" <smtp:wglick@manywhere.com>, "John Bentley" <smtp:JB@manywhere.com>, "Earl Woolley" <smtp:EWoey@manywhere.com>, "CaMail" <smtp:CaptureMail@manywhere.com>