Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > split()'s regex pattern parameter

Reply
Thread Tools

split()'s regex pattern parameter

 
 
Jerry Adair
Guest
Posts: n/a
 
      03-30-2006
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-30-2006
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....d952afae9e4b86

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

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      03-30-2006
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."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Regex to breakdown a pattern and use the pattern to breakdown a string ChrisC Perl Misc 4 06-25-2010 05:46 PM
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Python 8 09-26-2006 03:24 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Perl Misc 2 09-25-2006 03:15 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM



Advertisments