split()'s regex pattern parameter

Discussion in 'Perl Misc' started by Jerry Adair, Mar 30, 2006.

  1. Jerry Adair

    Jerry Adair Guest

    Hello-

    'Encountering something weird (well weird to me), 'couldn't find it in the
    FAQ:

    when attempting to call split() with a value to "split on" that is stored in
    a scalar, I get behavior other than what I expected. The Camel book says to
    just put it inside match delimiters, but that doesn't help the cause. Thus:

    @line = split( /$separator/ );

    doesn't do what I thought it would, as if $separator was replaced with:

    @line = split( " " );

    The problem I am encountering is when I try to access the list produced by
    the split (with the scalar), the first list value is null. However, with
    the simple string example (I know that split will parse a string as a
    pattern even when not given as a pattern) I get the correct results, which
    is a non-null list (for a given non-null line of data).

    I don't know why this pattern won't work in regex parser, I was surprised.
    I'm probably missing something simple, but I thought I'd ask anyway.

    Thank you in advance.

    Jerry
    Jerry Adair, Mar 30, 2006
    #1
    1. Advertising

  2. Jerry Adair

    Paul Lalli Guest

    Jerry Adair wrote:
    > 'Encountering something weird (well weird to me), 'couldn't find it in the
    > FAQ:
    >
    > when attempting to call split() with a value to "split on" that is stored in
    > a scalar, I get behavior other than what I expected. The Camel book says to
    > just put it inside match delimiters, but that doesn't help the cause. Thus:
    >
    > @line = split( /$separator/ );
    >
    > doesn't do what I thought it would, as if $separator was replaced with:
    >
    > @line = split( " " );
    >
    > The problem I am encountering is when I try to access the list produced by
    > the split (with the scalar), the first list value is null. However, with
    > the simple string example (I know that split will parse a string as a
    > pattern even when not given as a pattern) I get the correct results, which
    > is a non-null list (for a given non-null line of data).
    >
    > I don't know why this pattern won't work in regex parser, I was surprised.
    > I'm probably missing something simple, but I thought I'd ask anyway.


    This issue came up in this group about a month ago:
    http://groups.google.com/group/comp...952afae9e4b86?q=split&rnum=2#19d952afae9e4b86

    End result: a hardcoded literal space character is a special case.
    You cannot get the same behavior by assigning a space character to a
    variable and using that variable as your delimiter instead. It may be
    counter-intuitive, but that's the way it is.

    Paul Lalli
    Paul Lalli, Mar 30, 2006
    #2
    1. Advertising

  3. Jerry Adair

    Dr.Ruud Guest

    Jerry Adair schreef:

    > when attempting to call split() with a value to "split on" that is
    > stored in a scalar, I get behavior other than what I expected. The
    > Camel book says to just put it inside match delimiters, but that
    > doesn't help the cause. Thus:
    >
    > @line = split( /$separator/ );


    Where are the examples of the value of $separator?


    > doesn't do what I thought it would, as if $separator was replaced
    > with:
    >
    > @line = split( " " );
    >
    > The problem I am encountering is when I try to access the list
    > produced by the split (with the scalar), the first list value is
    > null.


    Does the PATTERN contain parentheses? See perldoc -f split.


    Also: <quote> A "split" on "/\s+/" is
    like a "split(' ')" except that any leading whitespace produces
    a null first field. A "split" with no arguments really does a
    "split(' ', $_)" internally. </quote>


    > However, with the simple string example (I know that split
    > will parse a string as a pattern even when not given as a pattern) I
    > get the correct results, which is a non-null list (for a given
    > non-null line of data).


    If your PATTERN is a single space, then you have a special case.


    > I don't know why this pattern won't work in regex parser, I was
    > surprised. I'm probably missing something simple, but I thought I'd
    > ask anyway.


    Did you try $separator = qr/PATTERN/ ?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Mar 30, 2006
    #3
    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. Xah Lee
    Replies:
    1
    Views:
    939
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    462
    Ilias Lazaridis
    Sep 26, 2006
  3. Ichiro
    Replies:
    8
    Views:
    681
    Roedy Green
    Jul 13, 2009
  4. Xah Lee
    Replies:
    2
    Views:
    218
    Xah Lee
    Sep 25, 2006
  5. ChrisC
    Replies:
    4
    Views:
    170
    ChrisC
    Jun 25, 2010
Loading...

Share This Page