Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: How to get split $var to work like split ' '?

by Laurent_R (Monsignor)
on Sep 10, 2013 at 17:19 UTC ( #1053324=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to get split $var to work like split ' '?
in thread How to get split $var to work like split ' '?

Now, I understand what you are looking for, it wasn't clear to me so far. If you store ' ' into a variable, you don't get the expected behavior as if you hard code:

my @fields = split ' ', $line;

Using "qr/\s+/;" as a split pattern improves the result but still does not yields what you want (you still get an extra empty element at the beginning of the array).

Well, then I don't see any real simple direct solution. Even something like this:

my @fields = split $pattern? $pattern : ' ', $line;

does not do what you are looking for. The only solutions I can think of are either to use the above "qr/\s+/;" and shift the first element if empty, or use a if else construct, or yet this equivalent construct:

my @fields  = defined $pattern? split $pattern, $line : split ' ', $line;


Comment on Re^3: How to get split $var to work like split ' '?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-31 05:18 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 (274 votes), past polls