Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Understanding Split and Join

by jwkrahn (Monsignor)
on Dec 28, 2006 at 13:23 UTC ( #592036=note: print w/ replies, xml ) Need Help??


in reply to Understanding Split and Join

There are cases where it is equally easy to use a regexp in list context to split a string as it is to use the split function. Consider the following examples:
my @list = split /\s+/, $string; my @list = $string =~ /(\S+)/g;
In the first example you're defining what to throw away. In the second, you're defining what to keep. But you're getting the same results. That is a case where it's equally easy to use either syntax.

In your regexp example you don't need the parentheses, it will work the same without them.

If $string contains leading whitespace then you will NOT get the same results. To demonstrate examples that produce the same results:

my @list = split ' ', $string; my @list = $string =~ /\S+/g;


Comment on Re: Understanding Split and Join
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2015-07-04 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls