Re: split()'s regex pattern parameter



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.lang.perl.misc/browse_frm/thread/6b0ff005ec96fc3f/19d952afae9e4b86?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

.



Relevant Pages

  • split()s regex pattern parameter
    ... The Camel book says to ... the split (with the scalar), the first list value is null. ... 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, ...
    (comp.lang.perl.misc)
  • Re: Using regexp groups in VB6
    ... A space character is ... a bad choice for a pair separator in a name/value pair string when spaces ... Many thanks for sharing the pattern. ...
    (microsoft.public.vb.general.discussion)
  • split()s pattern argument
    ... 'Encountering something weird, ... a scalar, I get behavior other than what I expected. ... with the simple string example (I know ... pattern) I get the correct results, which are simply the first word and the ...
    (comp.lang.perl.misc)
  • Re: Regular Expression
    ... the pattern first matches the space character (it ... > tries the part after the second "|", which matches the end of the string! ... > anchors in your pattern: ...
    (microsoft.public.scripting.vbscript)
  • Re: convert string to number
    ... maximum of two (its inherent type and a string equivalent) at any ... one, and a string one. ... every scalar data type keeps space for a pointer to its string ... There is indeed a simple way to test in Perl whether a scalar value is being ...
    (perl.beginners)