Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: split function not working as expected

by lonewolf28 (Beadle)
on Jul 28, 2015 at 04:10 UTC ( #1136543=note: print w/replies, xml ) Need Help??


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

Even if i backslash it i get the same....

my $string = 'osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA + rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53'; my ( $first, $second ) = split ( "\|", $string ); say "first:", $first; say "second:", $second; output: first:o second:s

Replies are listed 'Best First'.
Re^4: split function not working as expected
by AnomalousMonk (Archbishop) on Jul 28, 2015 at 05:08 UTC

    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:  <%-(-(-(-<

Re^4: split function not working as expected
by derby (Abbot) on Jul 28, 2015 at 11:43 UTC

    Right ... as I said, if you want to use quotes instead of a regex pattern, use single quotes. Double quotes cause interpolation to happen which you don't want here.

    -derby

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-02-06 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (37 votes). Check out past polls.

    Notices?