Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Empty strings after split /(\W)/

by Anonymous Monk
on Oct 01, 2014 at 21:09 UTC ( [id://1102591]=note: print w/replies, xml ) Need Help??


in reply to Empty strings after split /(\W)/

Think of it like a really simple CSV file*. Your separator character is \W. To make it easier to think about, replace all \W with a comma, and your input string is "Hello,,World,,,".

split /,/, "Hello,,World,,," gives you the list "Hello", "", "World" (trailing empty fields are stripped as documented).

What you're asking split to do when you say split /(,)/, "Hello,,World,,," is keep the separator character in the list of return values (also the empty fields between separators aren't stripped). Hence:

$ perl -e 'print join("|",split(/(\W)/,"Hello,,World,,,")),"\n";' Hello|,||,|World|,||,||,

* Just for the sake of discussion, we all know we should be using Text::CSV instead of split ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found