Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Shlomi Fish (918)

Shlomi Fish
  shlomif@iglu.org.il
http://www.shlomifish.org/
AOL IM: ShlomiFish (Add Buddy, Send Message)
Yahoo! ID: shlomif2 (Add User, Send Message)
Jabber: ShlomiFish@jabber.org

I'm a hacker of Perl, C, Shell, and occasionally other languages. Perl is my favourite language by far. I'm a member of the Israeli Perl Mongers, and contribute to and advocate open-source technologies. Technorati Profile [technorati.com]

Journal of Shlomi Fish (918)

Wednesday June 06, 2007
12:19 PM

Silly Trick: Split a String into Lines

[ #33449 ]

Wow! I have a lot to blog about Perl here, so let's start. First of all, a really trivial trick that I wondered about and that avar on Freenode taught me (thanks!): how to split a string into lines while preserving the trailing "\n". It's very simple: my @a = split(/^/, $text). One thing I wonder is why I don't get an empty line at the beginning of the string, because:

perl -le 'my @a=split(/p/,"pillow pillow on the pall");print join(",",@a)'

Gives me an empty string as the first argument. I also wonder if using split(/^/, $text, -1) instead, will do any difference.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Empty leading (or trailing) fields are produced when there are positive width matches at the beginning (or end) of the string; a zero-width match at the beginning (or end) of the string does not produce an empty field.