ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Got a bit of an odd one (I expect it has something to do with the data thats being passed in)
# my @split = split /\n|\r/, $message; my @split = split /[\n\r]/, $message; foreach (@split) { $_ =~ s/^[\n\r]//sig; $_ =~ s/[\n\r]$//sig; print "BLA ... \"$_\" <br />"; }
This outputs:
BLA ... "== another main header" BLA ... " " BLA ... " " BLA ... " dfgdfg" BLA ... " " BLA ... " === a sub header" BLA ... " dfgdfg" BLA ... " " BLA ... " === another new sub header" BLA ... " " BLA ... " sdfsdf" BLA ... " " BLA ... " ==== a sub-sub header" BLA ... " g" BLA ... " dsfgdsf" BLA ... " The actual code being passed in (via a textarea box), is: == another main header dfgdfg === a sub header dfgdfg === another new sub header sdfsdf ==== a sub-sub header g dsfgdsf
I just can't work out why its putting a extra newline breaks, where there shouldn't be (I want one line of data per line, and that it)

Any suggestions?

TIA

Andy

Replies are listed 'Best First'.
Re: Doing a split// but not working
by JavaFan (Canon) on Jan 05, 2010 at 13:10 UTC
    HTTP uses CRLF as line endings (CRLF is the common line ending for network protocols). You're splitting on *either* a carriage return *or* a linefeed. Effectively, you're adding a blank line for each line of input.

    Split on /[\n\r]+/ (but this doesn't keep blank input lines), /\r?\n/ (technically not quite correct), or, if you're using 5.10 (and why wouldn't you?), /\R/.

      Eugh, turned out was just me being stupid ;) Had this line near the top of the function:
             $message =~ s|\n|<br />|g;

      So what I need to do I should really just do before that regex = )

      Thanks for the help though

      Cheers

      Andy