split()'s regex pattern parameter

J

Jerry Adair

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
 
P

Paul Lalli

Jerry said:
'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
 
D

Dr.Ruud

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
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/ ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top