split()'s pattern argument

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 if the very first
"token" it encounters matches the value contained in the scalar. So for
simplicity, I set $separator = ' '; then when using this scalar in the call
to split(), it just so happens that the first line of data begins with a
space, followed by two "words" that are separated by whitespace. So the
list that I get back from split() has 3 elements in it: a null, the first
word and the second word. 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 are simply the first word and the
second word, thus the list I get back has only 2 elements in it, as it
should.

I don't know why I am experiencing this behavior. I'm probably missing
something simple, but I thought I'd ask anyway.

Thank you in advance.

Jerry
 
X

xhoster

Jerry Adair said:
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( " " );

split " " is a special case, and does not do the same thing as split / /.
A variable which contains a space will give you the split / / behavior, not
the special-case split " " behavior.

This was discussed to death here a couple of weeks ago.

Xho
 
U

usenet

Jerry said:
to split(), it just so happens that the first line of data begins with a
space, followed by two "words" that are separated by whitespace. So the
list that I get back from split() has 3 elements in it

If all you want to do is split $_ on whitespace ignoring any leading
whitespace the you can simply say "split". Observe:

#!/usr/bin/perl

use warnings; use strict;

$_ = ' foo bar ';
my @array = split;

print map {"'$_'\n"} @array;

__END__
 
J

Jerry Adair

Thanks David (and Xho),

Unfortunately I needed to do more than just split $_ on whitespace. I made
it work by calling split with the regex pattern that contained the scalar
and then simply checking the resultant list for a beginning NULL value. If
so, I just shifted to the next value and went on about my business. It
seems clug-eee to me, but it works and handles all of the test cases I've
thrown at it, so...

Thank y'all again.

Jerry
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top