Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RE: split function not working as expected[SOLVED]

by lonewolf28 (Beadle)
on Jul 28, 2015 at 00:48 UTC ( [id://1136532]=perlquestion: print w/replies, xml ) Need Help??

lonewolf28 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I've been trying out challenges in codeeval and in one of the challenges when i tried to split a string using "|" delimiter the split function wasn't working as it should.

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

what mistake am i making ? I'd expect the first variable to hold the random characters and second to hold the numbers.

Replies are listed 'Best First'.
Re^2: split function not working as expected
by kcott (Archbishop) on Jul 28, 2015 at 03:39 UTC

    G'day lonewolf28,

    If you look at the documentation for the split function, you'll see that the first argument to split is a "PATTERN".

    Your "PATTERN" is: zero-length-string OR zero-length-string

    And, with the last part being totally redundant, just: zero-length-string

    The output is entirely to be expected. The split documentation discusses outcomes with zero-length patterns.

    A better "PATTERN" would be: /[|]/

    See also: Perl Regular Expressions Tutorial

    — Ken

Re^2: split function not working as expected
by derby (Abbot) on Jul 28, 2015 at 01:18 UTC

    I believe you're running into some *special* cases when using a double-quoted literal and the pipe symbol. Normally the pipe symbol would be used for ORing your pattern. "i|A" would split on the characters i or A. When you want the pattern to match the pipe symbol, you would need to escape it with a backslash "\|" - however in this case when you double-quote and backslash, it is not escaped -- you need to single quote and backslash. I normally opt to use a regex as the PATTERN in split -- to me it's more readaable:

    my( $first, $second ) = split /\|/, $string;
    but that's just me.

    -derby

      My personal preference is to "escape" such metacharacters (and the space character) within a character class: [|]. This avoids incipient LTS and is also written the same in all variations:  /[|]/  "[|]"  '[|]'


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

      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

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

        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
Re^2: split function not working as expected
by Sathishkumar (Scribe) on Jul 28, 2015 at 08:02 UTC
    Hi, Try this below code.
    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 ); print "first:", $first,"\n"; print "second:", $second,"\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-29 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found