Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^4: split function not working as expected

by AnomalousMonk (Archbishop)
on Jul 28, 2015 at 05:08 UTC ( #1136546=note: print w/replies, xml ) Need Help??

in reply to Re^3: split function not working as expected
in thread RE: split function not working as expected[SOLVED]

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'first|second|third'; my @ra = split '[|]', $s; dd \@ra; " ["first", "second", "third"]
Also works with  "[|]" and  /[|]/

Update: The reason  "\|" doesn't work is that in double-quote interpolation, the  \ (backslash) acts, in this case, on the  | (pipe) to produce a literal pipe. Double-quotish interpolation would need a doubled backslash to yield a single literal backslash in the compiled string:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{\|}; print qq{:$s:}; ;; my $t = qq{\\|}; print qq{:$t:}; " :|: :\|:
That's why it's generally better to use  qr{pattern} to compile a regex. (I use  qq{...} in the example because Windows command line uses "..." to quote everything and  qq{...} reduces the need for backslashes.) Contrast with  '\|' and  '\\|' Please see Quote and Quote-like Operators and Quote-Like Operators.

Give a man a fish:  <%-(-(-(-<

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2023-02-05 11:53 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.