Re: split()'s regex pattern parameter



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."
.



Relevant Pages

  • Re: Using Regular Expression
    ... I actually read the contents to a scalar & match treating the scalar ... Is there some distinguishing pattern that separates each entry from the ...     ... ...
    (comp.lang.perl.modules)
  • Re: split()s regex pattern parameter
    ... 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, ... a hardcoded literal space character is a special case. ...
    (comp.lang.perl.misc)
  • Re: Having problems with with the s/// substitution
    ... > replace a pattern which exists multiple times in each line. ... Scalars in Perl have a special property, a regex cursor, accessible ... If you change a scalar using s/// the cursor for that string is reset. ...
    (comp.lang.perl.misc)
  • 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)