Split a string in perl

Discussion in 'Perl Misc' started by John, Feb 22, 2008.

  1. John

    John Guest

    There must be something that I have ignored in PERL.

    To split a string with delimiter "|", sometimes, I need to give "\",
    sometimes it works for "\\" only.

    Here are the details.

    The String is " 0000|Null".

    And the ways I have tried to do split are:
    1. my @columns = split('\|', $String);
    2. my ( $proj, $desc) = split('\|', $String);
    3. my ( $proj, $desc) = split('\\|', $String);

    Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
    receive an empty string .

    What was wrong with case 2 , I thought if Case 1 works, Case2 should
    be working also, Right ? I am using perl 5.6.1 on linux box.



    Thanks a lot for the tips.


    John.
     
    John, Feb 22, 2008
    #1
    1. Advertising

  2. On Feb 22, 11:01 am, John <> wrote:
    > There must be something that I have ignored in PERL.
    >
    > To split a string with delimiter "|", sometimes, I need to give "\",
    > sometimes it works for "\\" only.
    >
    > Here are the details.
    >
    > The String is " 0000|Null".
    >
    > And the ways I have tried to do split are:
    > 1. my @columns = split('\|', $String);
    > 2. my ( $proj, $desc) = split('\|', $String);
    > 3. my ( $proj, $desc) = split('\\|', $String);
    >
    > Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
    > receive an empty string .
    >
    > What was wrong with case 2 , I thought if Case 1 works, Case2 should
    > be working also, Right ? I am using perl 5.6.1 on linux box.
    >


    case 2 works as is. Is it possible you meant:
    split( "\|", $String ) ...?

    In that case, double-quotish interpolation causes
    your split regex to be just '|':

    # perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
    split(/|/, ' 0000|Null', 0);
    -e syntax OK

    Then, you have the alternation meta character
    alone with empty alternatives on either side
    which'll match the 1st 2 characters encountered:

    $proj=' ' $desc='0'

    Is it possible you had 2 leading blanks in
    string which "looked" like 2 empty strings for
    $proj and $desc...

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Feb 22, 2008
    #2
    1. Advertising

  3. John

    Ben Morrow Guest

    Quoth John <>:
    >
    > There must be something that I have ignored in PERL.
    >
    > To split a string with delimiter "|", sometimes, I need to give "\",
    > sometimes it works for "\\" only.
    >
    > Here are the details.
    >
    > The String is " 0000|Null".
    >
    > And the ways I have tried to do split are:
    > 1. my @columns = split('\|', $String);
    > 2. my ( $proj, $desc) = split('\|', $String);


    The first argument to split is a regex (with the special exception of
    ' '). Don't pass something that looks like a string, you'll just confuse
    yourself.

    my (@proj, $desc) = split(/\|/, $String);

    Ben
     
    Ben Morrow, Feb 22, 2008
    #3
  4. John

    John Guest

    On Feb 22, 11:56 am, "comp.llang.perl.moderated" <c...@blv-
    sam-01.ca.boeing.com> wrote:
    > On Feb 22, 11:01 am, John <> wrote:
    >
    >
    >
    >
    >
    > > There must be something that I have ignored in PERL.

    >
    > > To split a string with delimiter "|", sometimes, I need to give "\",
    > > sometimes it works for "\\" only.

    >
    > > Here are the details.

    >
    > > The String is " 0000|Null".

    >
    > > And the ways I have tried to do split are:
    > > 1. my @columns  = split('\|', $String);
    > > 2. my ( $proj, $desc) = split('\|', $String);
    > > 3. my ( $proj, $desc) = split('\\|', $String);

    >
    > > Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
    > > receive an empty string .

    >
    > > What was wrong with case 2 , I thought if Case 1 works, Case2 should
    > > be working also, Right ?  I am using perl 5.6.1 on linux box.

    >
    > case 2 works as is.  Is it possible you meant:
    > split( "\|", $String ) ...?
    >
    > In that case, double-quotish interpolation causes
    > your split regex to be just '|':
    >
    > # perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
    > split(/|/, ' 0000|Null', 0);
    > -e syntax OK
    >
    > Then, you have the alternation meta character
    > alone with empty alternatives on either side
    > which'll match the 1st 2 characters encountered:
    >
    >   $proj=' '  $desc='0'
    >
    > Is it possible you had 2 leading blanks in
    > string which "looked" like 2 empty strings for
    > $proj and $desc...
    >
    > --
    > Charles DeRykus- Hide quoted text -
    >
    > - Show quoted text -


    I do not have any leading blanks in this String.

    -John
     
    John, Feb 22, 2008
    #4
  5. John <> wrote:
    >
    >
    >There must be something that I have ignored in PERL.
    >
    >To split a string with delimiter "|", sometimes, I need to give "\",
    >sometimes it works for "\\" only.
    >
    >Here are the details.
    >
    >The String is " 0000|Null".
    >
    >And the ways I have tried to do split are:
    >1. my @columns = split('\|', $String);
    >2. my ( $proj, $desc) = split('\|', $String);
    >3. my ( $proj, $desc) = split('\\|', $String);
    >
    >Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
    >receive an empty string .


    I cannot reproduce your results:

    C:\tmp>type t.pl
    use strict; use warnings;
    my $String = " 0000|Null";
    my @columns = split('\|', $String);
    print "@columns\n";
    my ( $proj, $desc) = split('\|', $String);
    print $proj, ' ', $desc, "\n";
    ( $proj, $desc) = split('\\|', $String);
    print $proj, ' ', $desc , "\n";

    C:\tmp>t.pl
    0000 Null
    0000 Null
    0000 Null

    Each of versions produces the same result as the others.

    jue
     
    Jürgen Exner, Feb 22, 2008
    #5
  6. On Feb 22, 12:34 pm, John <> wrote:
    > On Feb 22, 11:56 am, "comp.llang.perl.moderated" <c...@blv-
    >
    >
    >
    > sam-01.ca.boeing.com> wrote:
    > > On Feb 22, 11:01 am, John <> wrote:

    >
    > > > There must be something that I have ignored in PERL.

    >
    > > > To split a string with delimiter "|", sometimes, I need to give "\",
    > > > sometimes it works for "\\" only.

    >
    > > > Here are the details.

    >
    > > > The String is " 0000|Null".

    >
    > > > And the ways I have tried to do split are:
    > > > 1. my @columns = split('\|', $String);
    > > > 2. my ( $proj, $desc) = split('\|', $String);
    > > > 3. my ( $proj, $desc) = split('\\|', $String);

    >
    > > > Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
    > > > receive an empty string .

    >
    > > > What was wrong with case 2 , I thought if Case 1 works, Case2 should
    > > > be working also, Right ? I am using perl 5.6.1 on linux box.

    >
    > > case 2 works as is. Is it possible you meant:
    > > split( "\|", $String ) ...?

    >
    > > In that case, double-quotish interpolation causes
    > > your split regex to be just '|':

    >
    > > # perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
    > > split(/|/, ' 0000|Null', 0);
    > > -e syntax OK

    >
    > > Then, you have the alternation meta character
    > > alone with empty alternatives on either side
    > > which'll match the 1st 2 characters encountered:

    >
    > > $proj=' ' $desc='0'

    >
    > > Is it possible you had 2 leading blanks in
    > > string which "looked" like 2 empty strings for
    > > $proj and $desc...

    >
    > > --
    > > Charles DeRykus- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > I do not have any leading blanks in this String.
    >


    Hm, the original post does however:

    > "The String is " 0000|Null".


    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Feb 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    492
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    739
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    239
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    277
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    708
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page