in reply to When is an ^$ not a ^$ ?

I'd go with dws on this one, $/ is a nice thing to be localizing, and often.

On the other hand, there are a couple of other approaches that are possible in this case. The first is to use my super-friend, the transliteration operator:

$file =~ tr/\r//d;

The other idea that comes to mind is a more complicated regex as an argument to split. I don't see this used often, which is a pity:

my @lines = split(/(?:\r?\n){2,}/, $file);

Take these with a grain of salt, because they could require some tweaking depending on your circumstances. Just remember that hammers in Perl usually have GPS and homing as well.

Replies are listed 'Best First'.
Re: Re: When is an code^$/code not a code^$/code?
by Clownburner (Monk) on Apr 05, 2001 at 06:10 UTC
    Another great suggestion!

    I tried both of those, but neither worked at all. Same results as before - everything got pushed into $data[0]. :-(

    I also tried dws' suggestion, with no results.

    I don't know what's wrong with this evil, twisted text file, but I can tell you it's got me up to HERE right now.

    And yet, 5 seconds in vi fixes it perfectly. I'm baffled.

    A hex editor sees this where the newlines would be:
    0d 0a 0d 0a

    Any clues?


    Signature void where prohibited by law.
      I'm pretty baffled that all these things don't work. Can you put your problem files online maybe?

      0d is hex for \r, 0a is hex for \n. I made myself a testfile by: perl -e 'print "aa\r\nab\r\n\r\naa\nbb\n"' >test Than I tried to get items out from it by: perl -e '$/="\r\n\r\n"; print "\tMy item: $_" while (<>);' <test This gave me the following output:

      My item: aa ab My item: aa bb
      So that seems to work. I don't see why this would work and the others not, so I tried another one:  perl -e 'undef $/;@a=split(/\r\n\r\n/,<>); print join "\t My item: ", @a;' <test Which gave a similar result. There must be something special to your case.

      Hope this helps a bit,

      Jeroen
      "We are not alone"(FZ)

        I'll try to recreate a file that has this problem that I can post; right now the files I'm working on are part of a security audit so the data can't really be posted.

        It is really weird though - the Hex editor confirms that there's nothing there but a bunch of \r\n's, and yet I can't split on \r\n. Funkadelic.

        BTW, this is under Perl 5.005_03, you don't suppose there might be a bug or something?


        Signature void where prohibited by law.