perl split

Discussion in 'Perl Misc' started by James, Aug 25, 2010.

  1. James

    James Guest

    This I understand,
    $ perl -le '@a=split//,"abc"; print $#a; print for(@a)'
    2
    a
    b
    c

    But why this behavior?
    $ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
    5

    a

    b

    c

    TIA
    -James
     
    James, Aug 25, 2010
    #1
    1. Advertising

  2. James

    Dr.Ruud Guest

    On 2010-08-25 19:56, James wrote:

    > This I understand,
    > $ perl -le '@a = split //, "abc"; print $#a; print for @a'
    > 2 [...]
    >
    > But why this behavior?
    > $ perl -le '@a = split /(.)/, "abc"; print $#a; print for @a'
    > 5 [...]


    See perldoc -f split, look for 'parentheses'.

    --
    Ruud
     
    Dr.Ruud, Aug 25, 2010
    #2
    1. Advertising

  3. James

    Klaus Guest

    On 25 août, 19:56, James <> wrote:
    > But why this behavior?
    > $ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
    > 5
    >
    > a
    >
    > b
    >
    > c


    Looks normal to me: you split with any character as separator, so you
    get:

    - an empty string before the separator 'a'
    - then, because you have put brackets into the regex, the separator
    ('a' in this case) is added
    - then another empty string between the two separators 'a' and 'b'
    - then, again, you have put brackets into the regex, so you get the
    second separator ('b' in this case)
    - then yet another empty string between the two separators 'b' and 'c'
    - finally you get the last separator 'c'
     
    Klaus, Aug 25, 2010
    #3
  4. Klaus wrote:
    > On 25 août, 19:56, James<> wrote:
    >> But why this behavior?
    >> $ perl -le '@a=split/(.)/,"abc"; print $#a; print for(@a)'
    >> 5
    >>
    >> a
    >>
    >> b
    >>
    >> c

    >
    > Looks normal to me: you split with any character as separator, so you
    > get:
    >
    > - an empty string before the separator 'a'
    > - then, because you have put brackets into the regex, the separator
    > ('a' in this case) is added
    > - then another empty string between the two separators 'a' and 'b'
    > - then, again, you have put brackets into the regex, so you get the
    > second separator ('b' in this case)
    > - then yet another empty string between the two separators 'b' and 'c'
    > - finally you get the last separator 'c'


    And finally you get the last empty string between 'c' and the
    end-of-string, which is discarded because it is a trailing empty string.
    If you want to see it use a negative number as the third argument to
    split:

    $ perl -le '@a=split/(.)/,"abc",-1; print $#a; print for(@a)'
    6

    a

    b

    c




    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
     
    John W. Krahn, Aug 26, 2010
    #4
    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:
    483
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    724
    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:
    230
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    263
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    678
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page