Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Split, variable delimiter

6 views
Skip to first unread message

Heath

unread,
Feb 21, 2006, 1:11:56 PM2/21/06
to
Hello,

I'm using perl v5.8.7.

When $_ is set to something like "\t\t\t4.34\t4.65\t3.25\t9.54\n",
I do the following:

my @line = split ' ';
print "$#line, $line[0]\n";

And get what I expect:

3, 4.34

But when I do this:

my $delim = ' ';
my @line = split $delim;
print "$#line, $line[0]\n";
if ($line[0] eq $_) {print "Equal\n"; }

I get this:

0, 4.34 4.65
3.25 9.54

Equal

Which is not exactly what I'm after. So, why are these two
snippets behaving differently? What can I do to make them
equivalent?

I'm probably missing something obvious. If so, please be nice. ;)

use...@davidfilmer.com

unread,
Feb 21, 2006, 1:43:08 PM2/21/06
to
Heath wrote:
> my $delim = ' ';
> my @line = split $delim;

You still need delims around your delim. Try something like /$delim/

--
http://DavidFilmer.com

it_says_BALLS_on_your forehead

unread,
Feb 21, 2006, 2:25:58 PM2/21/06
to

Heath wrote:
> Hello,
>
> I'm using perl v5.8.7.
>
> When $_ is set to something like "\t\t\t4.34\t4.65\t3.25\t9.54\n",
> I do the following:
>
> my @line = split ' ';
> print "$#line, $line[0]\n";
>
> And get what I expect:
>
> 3, 4.34
>
> But when I do this:
>
> my $delim = ' ';
> my @line = split $delim;
> print "$#line, $line[0]\n";
> if ($line[0] eq $_) {print "Equal\n"; }
>
> I get this:
>
> 0, 4.34 4.65
> 3.25 9.54
>
> Equal
>
> Which is not exactly what I'm after. So, why are these two
> snippets behaving differently? What can I do to make them
> equivalent?

first, look at: perldoc -f split | more
or, browser-friendly:
http://www.physiol.ox.ac.uk/Computing/Online_Documentation/Perl-5.8.6/functions/split.html

see the part about the special case of split ' '

i would say the best thing to do would be not to use split ' ' at all.
==============
use strict; use warnings;

$_ = "\t\t\t4.34\t4.65\t3.25\t9.54\n";

$_ =~ s/^\s+//;

my @line = split /\s/;


print "$#line, $line[0]\n";

my $delim = '\s';
my @line2 = split /$delim/;
print "$#line2, $line2[0]\n";

Heath

unread,
Feb 21, 2006, 2:26:24 PM2/21/06
to
I've tried using $delim, /$delim/, "$delim", '$delim', and even
"\'$delim\'". All do the same as when using $delim.

Heath

unread,
Feb 21, 2006, 3:50:06 PM2/21/06
to

it_says_BALLS_on_your forehead wrote:
> first, look at: perldoc -f split | more
> or, browser-friendly:
> http://www.physiol.ox.ac.uk/Computing/Online_Documentation/Perl-5.8.6/functions/split.html
>
> see the part about the special case of split ' '

Yes, I read through that before I ever posted. The behavior I'm
after is that of [split ' ']. I don't get that behavior when I pass
the space char to split via a variable. I would simply just like to
know why that is and how I can get that behavior by passing a variable,
if it is possible at all.

> i would say the best thing to do would be not to use split ' ' at all.
> ==============
> use strict; use warnings;
>
> $_ = "\t\t\t4.34\t4.65\t3.25\t9.54\n";
>
> $_ =~ s/^\s+//;
>
> my @line = split /\s/;
> print "$#line, $line[0]\n";
>
> my $delim = '\s';
> my @line2 = split /$delim/;
> print "$#line2, $line2[0]\n";

This works great, but it accomplishes the exact same thing as:

==============
use strict; use warnings;

$_ = "\t\t\t4.34\t4.65\t3.25\t9.54\n";

my @line = split;


print "$#line, $line[0]\n";

==============

All I need is a value to assign to $delim such that a [split $delim]
will give me the same behavior as a [split].

Uri Guttman

unread,
Feb 21, 2006, 3:58:06 PM2/21/06
to
>>>>> "H" == Heath <hdew...@maxtc.com> writes:

H> Yes, I read through that before I ever posted. The behavior I'm
H> after is that of [split ' ']. I don't get that behavior when I pass
H> the space char to split via a variable. I would simply just like to
H> know why that is and how I can get that behavior by passing a variable,
H> if it is possible at all.

rtfm some more:

As a special case, specifying a PATTERN of space
(' ') will split on white space just as "split" with
no arguments does. Thus, "split(' ')" can be used
to emulate awk's default behavior, ...

note that PATTERN is the actual literal passed to split so it can't be a
variable. otherwise how could it tell / / from ' ' from $foo = ' '? this
is a very odd way to get that special behavior as it is inband and very
special cased. if you must have that vs other splits on demand, use a
sub to handle your cased and do 2 different splits based on $foo eq '
'. or use code refs for each split type. many ways to handle it.

H> All I need is a value to assign to $delim such that a [split $delim]
H> will give me the same behavior as a [split].

can't be done. so choose another solution.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

robic0

unread,
Feb 21, 2006, 5:51:29 PM2/21/06
to


It is a bug (I mean a feature) of split. According to the docs
the Perl parser seems to look for the single quoted space ' ' and that
differentiates it from a space " " as a pattern. So obviously the single
quote is as significant as the space. However, the 3 character string is
parsed as a first level parse. So you can't even assign $delim = "' '" or
$delim = "" (null string) ... which is a special case as well ass the ' '.

It would appear to be a very useful case since it populates every array position
with a non-whitespace. If you want to run dynamic patterns, the ' ' case will have
to be an exclusion, tested for and hardcoded.

ie: if ($delim ne ' ') {split $delim;} else {split ' '}

Quote:


As a special case, specifying a PATTERN of space (' ') will split on white space
just as split with no arguments does. Thus, split(' ') can be used to emulate awk's

default behavior, whereas split(/ /) will give you as many null initial fields as
there are leading spaces. 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.

Some code:

use strict;
use warnings;


$_ = "\t\t\t4.34\t4.65\t3.25\t9.54\n";

my @line = split /' '/;
print "$#line", @line, "\n";


if ($line[0] eq $_) {print "Equal\n"; }

my $dlim = ' ';
@line = split /$dlim/;
print "$#line", @line, "\n";


if ($line[0] eq $_) {print "Equal\n"; }

print "-------------\n";

@line = split ' ';
print "$#line", @line, "\n";


if ($line[0] eq $_) {print "Equal\n"; }

$dlim = '\s+';
@line = split /$dlim/;
print "$#line", @line, "\n";


if ($line[0] eq $_) {print "Equal\n"; }

print "---------\nbut\n";
$dlim = ' ';
@line = split /$dlim/;
print "$#line", @line, "\n";


if ($line[0] eq $_) {print "Equal\n"; }

__END__
Output:
0 4.34 4.65 3.25 9.54

Equal
0 4.34 4.65 3.25 9.54

Equal
-------------
34.344.653.259.54
44.344.653.259.54
---------
but
0 4.34 4.65 3.25 9.54

Equal

robic0

unread,
Feb 21, 2006, 6:26:33 PM2/21/06
to
On Tue, 21 Feb 2006 15:58:06 -0500, Uri Guttman <u...@stemsystems.com> wrote:

>>>>>> "H" == Heath <hdew...@maxtc.com> writes:
>
> H> Yes, I read through that before I ever posted. The behavior I'm
> H> after is that of [split ' ']. I don't get that behavior when I pass
> H> the space char to split via a variable. I would simply just like to
> H> know why that is and how I can get that behavior by passing a variable,
> H> if it is possible at all.
>
>rtfm some more:
>
> As a special case, specifying a PATTERN of space
> (' ') will split on white space just as "split" with
> no arguments does. Thus, "split(' ')" can be used
> to emulate awk's default behavior, ...
>
>note that PATTERN is the actual literal passed to split so it can't be a
>variable. otherwise how could it tell / / from ' ' from $foo = ' '? this

I don't know, I would consider this a bug, aka, left out check. Within split, if
the $foo name is passed as a literal name, the contents have to be obtained.
So if $foo = "' '", it should be fairly obvious what the meaning is.

But I don't think some intrinsics work that way. I think as far as the Pattern
in split, the parser looks for a split ' ' or split pattern and internally changes
the call to a different function with different parameters, than any other form of split.
There may be several internal split functions.
Since it has to be parsed anyway, its easier to redirect different "forms" to predefined
functions that handle specific ones. Thereby speeding up the processor.

Uri Guttman

unread,
Feb 21, 2006, 6:52:51 PM2/21/06
to
>>>>> "r" == robic0 <robic0> writes:


r> I don't know, I would consider this a bug, aka, left out
r> check. Within split, if the $foo name is passed as a literal name,
r> the contents have to be obtained. So if $foo = "' '", it should be
r> fairly obvious what the meaning is.

i consider you a genomic bug.

r> But I don't think some intrinsics work that way. I think as far as
r> the Pattern in split, the parser looks for a split ' ' or split
r> pattern and internally changes the call to a different function
r> with different parameters, than any other form of split. There may
r> be several internal split functions. Since it has to be parsed
r> anyway, its easier to redirect different "forms" to predefined
r> functions that handle specific ones. Thereby speeding up the
r> processor.

speeding up the processor? what kind of crack are you smoking? this
whole discussion has nothing to do with the speed of split. the various
special behaviors of split do not need seperate implmentations.

just another useless reply to a troll,

robic0

unread,
Feb 21, 2006, 7:17:25 PM2/21/06
to
On Tue, 21 Feb 2006 18:52:51 -0500, Uri Guttman <u...@stemsystems.com> wrote:

>>>>>> "r" == robic0 <robic0> writes:
>
>
> r> I don't know, I would consider this a bug, aka, left out
> r> check. Within split, if the $foo name is passed as a literal name,
> r> the contents have to be obtained. So if $foo = "' '", it should be
> r> fairly obvious what the meaning is.
>
>i consider you a genomic bug.
>
> r> But I don't think some intrinsics work that way. I think as far as
> r> the Pattern in split, the parser looks for a split ' ' or split
> r> pattern and internally changes the call to a different function
> r> with different parameters, than any other form of split. There may
> r> be several internal split functions. Since it has to be parsed
> r> anyway, its easier to redirect different "forms" to predefined
> r> functions that handle specific ones. Thereby speeding up the
> r> processor.
>
>speeding up the processor? what kind of crack are you smoking? this
>whole discussion has nothing to do with the speed of split. the various
>special behaviors of split do not need seperate implmentations.
>

I'm speechless.. You just discounted all compiled and semi-compiled (fixup)
languages. You must think Perl core is written in Perl.

The "Processor" is commonly known as the "engine", the core. Perl follows
that and has multiple core implementations of intrinsics, it does a modified
compile at loadtime and further compiles at runtime. Try compiling
C/C++ code with standard library calls, then look at the assembly.

thrill5

unread,
Feb 21, 2006, 7:26:12 PM2/21/06
to
Can be done. Set $delim to "\s" not ' '.

Scott

"Uri Guttman" <u...@stemsystems.com> wrote in message
news:x7pslgx...@mail.sysarch.com...

xho...@gmail.com

unread,
Feb 21, 2006, 8:58:43 PM2/21/06
to
robic0 wrote:

>
> It is a bug (I mean a feature) of split. According to the docs

Uh, what part of the docs are you refering to?

> the Perl parser seems to look for the single quoted space ' ' and that
> differentiates it from a space " " as a pattern.

It does not. ' ', " ", q{ }, qq{ } are all the same in this context. They
are differentiated from / /, qr{ }, and $x where $x eq ' ';

> So obviously the single
> quote is as significant as the space.

$ perl -le 'my $x=" foo bar "; print ":$_:" foreach split " ", $x'
:foo:
:bar:
$ perl -le 'my $x=" foo bar "; print ":$_:" foreach split q{ }, $x'
:foo:
:bar:
$ perl -le 'my $x=" foo bar "; print ":$_:" foreach split qq{ }, $x'
:foo:
:bar:
$ perl -le 'my $x=" foo bar "; print ":$_:" foreach split qr{ }, $x'
::
:foo:
:bar:
$ perl -le 'my $x=" foo bar "; print ":$_:" foreach split / /, $x'
::
:foo:
:bar:

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

robic0

unread,
Feb 21, 2006, 9:17:24 PM2/21/06
to
On 22 Feb 2006 01:58:43 GMT, xho...@gmail.com wrote:

>robic0 wrote:
>
>>
>> It is a bug (I mean a feature) of split. According to the docs
>
>Uh, what part of the docs are you refering to?
>
>> the Perl parser seems to look for the single quoted space ' ' and that
>> differentiates it from a space " " as a pattern.
>
>It does not. ' ', " ", q{ }, qq{ } are all the same in this context. They
>are differentiated from / /, qr{ }, and $x where $x eq ' ';
>

split ' '; is NOT the same context as split " "; nor
$dl = ' '; split $dl;

The discussion is about the special parsing done for "split ' '" context,
it is not about what, for exapmple split $dl; where $dl = ' '; means.

Wake up man..........

Heres the posting quotes of some internal posts (which the argument hinges):
If you don't want to read this, the jist is its a compiler with multiple form
intrisick functions.......
==============================================

On Tue, 21 Feb 2006 15:58:06 -0500, Uri Guttman <u...@stemsystems.com> wrote:

>>>>>> "H" == Heath <hdew...@maxtc.com> writes:
>
> H> Yes, I read through that before I ever posted. The behavior I'm
> H> after is that of [split ' ']. I don't get that behavior when I pass
> H> the space char to split via a variable. I would simply just like to
> H> know why that is and how I can get that behavior by passing a variable,
> H> if it is possible at all.
>
>rtfm some more:
>

> As a special case, specifying a PATTERN of space
> (' ') will split on white space just as "split" with
> no arguments does. Thus, "split(' ')" can be used

> to emulate awk's default behavior, ...
>
>note that PATTERN is the actual literal passed to split so it can't be a
>variable. otherwise how could it tell / / from ' ' from $foo = ' '? this

I don't know, I would consider this a bug, aka, left out check. Within split, if
the $foo name is passed as a literal name, the contents have to be obtained.
So if $foo = "' '", it should be fairly obvious what the meaning is.

But I don't think some intrinsics work that way. I think as far as the Pattern
in split, the parser looks for a split ' ' or split pattern and internally changes
the call to a different function with different parameters, than any other form of split.
There may be several internal split functions.
Since it has to be parsed anyway, its easier to redirect different "forms" to predefined
functions that handle specific ones. Thereby speeding up the processor.

>is a very odd way to get that special behavior as it is inband and very
>special cased. if you must have that vs other splits on demand, use a
>sub to handle your cased and do 2 different splits based on $foo eq '
>'. or use code refs for each split type. many ways to handle it.
>
> H> All I need is a value to assign to $delim such that a [split $delim]
> H> will give me the same behavior as a [split].
>
>can't be done. so choose another solution.
>
>uri

====================

On 21 Feb 2006 10:11:56 -0800, "Heath" <hdew...@maxtc.com> wrote:

>Hello,
>
> I'm using perl v5.8.7.
>
> When $_ is set to something like "\t\t\t4.34\t4.65\t3.25\t9.54\n",
> I do the following:
>
> my @line = split ' ';
> print "$#line, $line[0]\n";
>
> And get what I expect:
>
> 3, 4.34
>
> But when I do this:
>
> my $delim = ' ';
> my @line = split $delim;
> print "$#line, $line[0]\n";
> if ($line[0] eq $_) {print "Equal\n"; }
>
> I get this:
>
> 0, 4.34 4.65
> 3.25 9.54
>
> Equal
>
> Which is not exactly what I'm after. So, why are these two
> snippets behaving differently? What can I do to make them
> equivalent?
>
> I'm probably missing something obvious. If so, please be nice. ;)

It is a bug (I mean a feature) of split. According to the docs

the Perl parser seems to look for the single quoted space ' ' and that

robic0

unread,
Feb 21, 2006, 10:04:26 PM2/21/06
to

OR it does just a HARDCODED replacement of the "split ' '" form!
Which is a hack patch of some feeble minded Perl programmer.
There are no other options at all!!
Eh?

Uri Guttman

unread,
Feb 21, 2006, 11:44:59 PM2/21/06
to
>>>>> "t" == thrill5 <nos...@comcast.net> writes:

t> Can be done. Set $delim to "\s" not ' '.

rtfm some more please. first off you want \s+ to get something like
split ' '. and ' ' is not only special cased for splitting on any
whitespace but it emulates awk's behavior of skipping leading white
space.

If EXPR is omitted, splits the $_ string. If PATTERN is also
omitted, splits on whitespace (after skipping any leading
whitespace). Anything matching PATTERN is taken to be a
delimiter separating the fields. (Note that the delimiter may
be longer than one character.)

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.

hmm, seems to me that my point about not being able to pass in ' ' as a
value still holds up. did you think if it were just a special value
then they would not document it as such? it is not even a value since
the split is done with \s+ it is not splitting on ' ' (which is a
single space). so do you have another comment on this?

Uri Guttman

unread,
Feb 21, 2006, 11:49:27 PM2/21/06
to
>>>>> "r" == robic0 <robic0> writes:

r> I'm speechless.. You just discounted all compiled and semi-compiled
r> (fixup) languages. You must think Perl core is written in Perl.

albeit you were keyboardless as well. we must look into fixing that.

r> The "Processor" is commonly known as the "engine", the core. Perl
r> follows that and has multiple core implementations of intrinsics,
r> it does a modified compile at loadtime and further compiles at
r> runtime. Try compiling C/C++ code with standard library calls, then
r> look at the assembly.

as that statement shows a total lack of knowledge about perl's
internals, i will treat your comment with the respect it deserves:

BLUBLBBLBABLUBALLABEELALABEBBE!

and please don't ever try to teach me (hell, anyone) about computer
stuff. i have farted away more computer knowledge than you could ever
breathe in. but you should try to breathe in some anyhow as it will help
you for sure.

Bo Lindbergh

unread,
Feb 22, 2006, 4:59:03 AM2/22/06
to
In article <x7pslgx...@mail.sysarch.com>,
Uri Guttman <u...@stemsystems.com> wrote:

> rtfm some more:
>
> As a special case, specifying a PATTERN of space
> (' ') will split on white space just as "split" with
> no arguments does. Thus, "split(' ')" can be used
> to emulate awk's default behavior, ...
>
> note that PATTERN is the actual literal passed to split so it can't be a
> variable. otherwise how could it tell / / from ' ' from $foo = ' '?

By way of the qr construct. In a hypothetical future Perl version

my $delim=' ';
my @fields=split($delim);

would be equivalent to

my @fields=split(' ');

while

my $delim=qr/ /;
my @fields=split($delim);

would be equivalent to

my @fields=split(/ /);

and everyone would be happy. Right?


/Bo Lindbergh

Aaron Baugher

unread,
Feb 22, 2006, 8:25:52 AM2/22/06
to
Uri Guttman <u...@stemsystems.com> writes:

> rtfm some more please. first off you want \s+ to get something like
> split ' '. and ' ' is not only special cased for splitting on any
> whitespace but it emulates awk's behavior of skipping leading white
> space.

The documentation explains this quite well. However, it never says
that ' ' can't be passed in a variable, as far as I could find. Since
this is such a special case (Is there any other case in perl when this
is true?), perhaps the documentation should have a few lines added to
make that clear. Something like:

This special case only works when a single space is given to split
literally. Passing a single space in a variable causes split to
split on a single space, instead of using this special behavior.

$delim = ' ';
@s = split $delim, ' A B CD E';

Results in @s = ( '', 'A', 'B', 'CD', 'E' );

I'm sure that could be worded more accurately.


--
Aaron -- aaron_...@yahoo.com
http://360.yahoo.com/aaron_baugher

xho...@gmail.com

unread,
Feb 22, 2006, 11:13:04 AM2/22/06
to
robic0 wrote:
> On Tue, 21 Feb 2006 18:17:24 -0800, robic0 wrote:
>
> >On 22 Feb 2006 01:58:43 GMT, xho...@gmail.com wrote:
> >
> >>robic0 wrote:
> >>
> >>>
> >>> It is a bug (I mean a feature) of split. According to the docs
> >>
> >>Uh, what part of the docs are you refering to?
> >>
> >>> the Perl parser seems to look for the single quoted space ' ' and
> >>> that differentiates it from a space " " as a pattern.
> >>
> >>It does not. ' ', " ", q{ }, qq{ } are all the same in this context.
> >>They are differentiated from / /, qr{ }, and $x where $x eq ' ';
> >>
> >split ' '; is NOT the same context as split " ";

Well, at least you were smart enough to snip the examples proving you were
wrong before you re-iterated your lies.

Uri Guttman

unread,
Feb 22, 2006, 12:09:59 PM2/22/06
to
>>>>> "AB" == Aaron Baugher <abau...@esc.pike.il.us> writes:

AB> Uri Guttman <u...@stemsystems.com> writes:
>> rtfm some more please. first off you want \s+ to get something like
>> split ' '. and ' ' is not only special cased for splitting on any
>> whitespace but it emulates awk's behavior of skipping leading white
>> space.

AB> The documentation explains this quite well. However, it never says
AB> that ' ' can't be passed in a variable, as far as I could find. Since
AB> this is such a special case (Is there any other case in perl when this
AB> is true?), perhaps the documentation should have a few lines added to
AB> make that clear. Something like:

AB> This special case only works when a single space is given to split
AB> literally. Passing a single space in a variable causes split to
AB> split on a single space, instead of using this special behavior.

AB> $delim = ' ';
AB> @s = split $delim, ' A B CD E';

AB> Results in @s = ( '', 'A', 'B', 'CD', 'E' );

AB> I'm sure that could be worded more accurately.

i agree the docs are not the best on this special case.

and as they say patches welcome and perl5porters is over there --->

:)

Heath

unread,
Feb 22, 2006, 12:13:20 PM2/22/06
to
Uri Guttman wrote:
> >>>>> "t" == thrill5 <nos...@comcast.net> writes:
>
> t> Can be done. Set $delim to "\s" not ' '.
>
> rtfm some more please. first off you want \s+ to get something like
> split ' '. and ' ' is not only special cased for splitting on any
> whitespace but it emulates awk's behavior of skipping leading white
> space.

Just setting $delim = "\s" or "\s+" gives me a 'Unrecognized escape \s'
warning. '\s+' works nicely but, as you stated, still doesn't take
care of the leading whitespaces which is what I'd like.

Bo Lindbergh wrote:
> In article <x7pslgx...@mail.sysarch.com>,
> Uri Guttman <u...@stemsystems.com> wrote:
>

> > rtfm some more:
> >
> > As a special case, specifying a PATTERN of space
> > (' ') will split on white space just as "split" with
> > no arguments does. Thus, "split(' ')" can be used
> > to emulate awk's default behavior, ...
> >
> > note that PATTERN is the actual literal passed to split so it can't be a
> > variable. otherwise how could it tell / / from ' ' from $foo = ' '?
>

> By way of the qr construct. In a hypothetical future Perl version
>
> my $delim=' ';
> my @fields=split($delim);
>
> would be equivalent to
>
> my @fields=split(' ');
>
> while
>
> my $delim=qr/ /;
> my @fields=split($delim);
>
> would be equivalent to
>
> my @fields=split(/ /);
>
> and everyone would be happy. Right?
>
>
> /Bo Lindbergh

This is actually how I thought it would work, but until your
hypothetical version of Perl is released I guess I'll have to:

my @fields = split ($delim ? $delim : ' ');

which is decidedly less pretty.

Aaron Baugher wrote:
> The documentation explains this quite well. However, it never says
> that ' ' can't be passed in a variable, as far as I could find. Since
> this is such a special case (Is there any other case in perl when this
> is true?), perhaps the documentation should have a few lines added to
> make that clear.

I think that would definitely help. This actually got pretty annoying
until I caved in and posted.


Thanks to all for helping clear this up.

Uri Guttman

unread,
Feb 22, 2006, 12:23:18 PM2/22/06
to
>>>>> "H" == Heath <hdew...@maxtc.com> writes:

H> Uri Guttman wrote:
>> >>>>> "t" == thrill5 <nos...@comcast.net> writes:
>>
t> Can be done. Set $delim to "\s" not ' '.
>>
>> rtfm some more please. first off you want \s+ to get something like
>> split ' '. and ' ' is not only special cased for splitting on any
>> whitespace but it emulates awk's behavior of skipping leading white
>> space.

H> Just setting $delim = "\s" or "\s+" gives me a 'Unrecognized escape \s'
H> warning. '\s+' works nicely but, as you stated, still doesn't take
H> care of the leading whitespaces which is what I'd like.

be careful of what quote chars you use there. the 2 first ones are in ""
which will eat \ stuff and it is probably what is spitting out the error
as \s is not a valid escape char in "" string (like \n is). the ''
version doesn't look at \ (except before \ and ') so your regex gets
passed thru as is. better yet is to use qr// as it will even make sure
you pass in a clean regex.

H> Bo Lindbergh wrote:
>> By way of the qr construct. In a hypothetical future Perl version
>> my $delim=' ';
>> my @fields=split($delim);
>> would be equivalent to
>> my @fields=split(' ');
>>

>> my $delim=qr/ /;
>> my @fields=split($delim);
>> would be equivalent to
>> my @fields=split(/ /);
>>
>> and everyone would be happy. Right?

H> This is actually how I thought it would work, but until your
H> hypothetical version of Perl is released I guess I'll have to:

H> my @fields = split ($delim ? $delim : ' ');

H> which is decidedly less pretty.

since that only tests $delim for perl truth, you can just use:

@fields = split ($delim || ' ');


BTW, you should not make one followup to three different posts (and from
three different posters). it only confuses the threads.

Tad McClellan

unread,
Feb 22, 2006, 4:03:37 PM2/22/06
to

[ Please do not top-post.
Text rearranged into a sensible order.
]


thrill5 <nos...@comcast.net> wrote:
> "Uri Guttman" <u...@stemsystems.com> wrote in message
> news:x7pslgx...@mail.sysarch.com...
>>>>>>> "H" == Heath <hdew...@maxtc.com> writes:
>>
>> H> Yes, I read through that before I ever posted. The behavior I'm
>> H> after is that of [split ' '].

>> H> All I need is a value to assign to $delim such that a [split $delim]
>> H> will give me the same behavior as a [split].
>>
>> can't be done. so choose another solution.

> Can be done.


OK, let's see it then!

Got code that "does" it?

I think not.


> Set $delim to "\s" not ' '.


The OP wants to ignore the leading empty fields, your suggestion
does not ignore the leading empty fields.


perl -le '$_="\t\t\tone\ttwo\tthree"; print for split'
one
two
three


perl -le '$_="\t\t\tone\ttwo\tthree"; $delim = "\s"; print for split $delim'
one two three
(there is no 's' to split on)

perl -le '$_="\t\t\tone\ttwo\tthree"; $delim = q(\s); print for split $delim'

one
two
three
(3 leading empty fields)

perl -le '$_="\t\t\tone\ttwo\tthree"; $delim = "\s"; print for split /\s/'

one
two
three
(3 leading empty fields again)

If you do not understand the question, it is best to refrain
from answering it.


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

thrill5

unread,
Feb 22, 2006, 6:25:38 PM2/22/06
to
To Uri,

Yes, I do have a few more comments.

1) Your an ASSHOLE.

2) The answer was better than 'Can't be done'.

3) If jumping in my shit makes you feel like more of "programmer" than I
feel sorry for you and take back the 'ASSHOLE' comment.

4) Posting messages is a way to learn more and your "holier-than-thou'
attitude makes some people uncomfortable and they so they don't post. I am
not one of those people. Next time, please for the sake of the rest of us,
leave the attitude at the door.

Scott
"Uri Guttman" <u...@stemsystems.com> wrote in message

news:x7r75wv...@mail.sysarch.com...

A. Sinan Unur

unread,
Feb 22, 2006, 7:15:25 PM2/22/06
to
"thrill5" <nos...@comcast.net> wrote in
news:eYOdnX5U2NK...@comcast.com:

> To Uri,
>
> Yes, I do have a few more comments.
>
> 1) Your an

You might want to learn how to spell properly if you are going to call
people names.

....

> Next time, please for the sake of the
> rest of us, leave the attitude at the door.


Good advice for you. Bye.

Sinan
--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

thrill5

unread,
Feb 22, 2006, 9:29:22 PM2/22/06
to
Wow! Two birds with one stone!!

Scott
"A. Sinan Unur" <1u...@llenroc.ude.invalid> wrote in message
news:Xns9772C3FDAF57...@127.0.0.1...

Uri Guttman

unread,
Feb 23, 2006, 1:01:55 AM2/23/06
to
>>>>> "t" == thrill5 <nos...@comcast.net> writes:

t> To Uri,
t> Yes, I do have a few more comments.

t> 1) Your an ASSHOLE.

s/your/you're/. please spell your insults correctly if you want them to
have the maximum effect.

t> 2) The answer was better than 'Can't be done'.

but it was wrong. hmmm which is better, a long and wrong answer or short
correct one. tough choice. think about it. don't hurt yourself doing it.

t> 3) If jumping in my shit makes you feel like more of "programmer" than I
t> feel sorry for you and take back the 'ASSHOLE' comment.

i have no need for any emotional connection with you. i was working in
the perl universe. too bad you don't get that. i don't feel sorry for
you.

t> 4) Posting messages is a way to learn more and your
t> "holier-than-thou' attitude makes some people uncomfortable and
t> they so they don't post. I am not one of those people. Next time,
t> please for the sake of the rest of us, leave the attitude at the
t> door.

hmm. pot meet kettle. in the world of programming, accuracy and
correctness are generally appreciated more than social niceties. but
then you may want to be told sweet lies about how great your code is and
how it satisfies the requirements. i prefer to have correct code and i
am open to anyone who could improve my work. kinda puts the work over my
personal glory. but you wouldn't understand that.

<snip of entire botton quote>

and you still don't know about top posting. when will you learn
anything!?!

have fun!

Paul Lalli

unread,
Feb 23, 2006, 8:23:40 AM2/23/06
to
Uri Guttman wrote:
> s/your/you're/. please spell your insults correctly if you want them to
> have the maximum effect.

While I do, of course, agree whole-heartily with your entire post, I
can help but point out my amusement that someone who flat out refuses
to use the shift key to correctly capitalize his sentences is
complaining about someone else's spelling... ;-)

Paul Lalli

it_says_BALLS_on_your_forehead

unread,
Feb 23, 2006, 8:28:12 AM2/23/06
to

you "can help" but choose not to, apparently ;-).

Paul Lalli

unread,
Feb 23, 2006, 8:32:52 AM2/23/06
to

You know... I went over that post 3 times before submitting, because I
*really* wanted to avoid anyone calling me on my calling someone else
on their calling someone else on grammatical and spelling mistakes....
and I completely missed an entire lack of "'t". Sheesh.

Paul Lalli

Uri Guttman

unread,
Feb 23, 2006, 11:59:49 AM2/23/06
to
>>>>> "PL" == Paul Lalli <mri...@gmail.com> writes:

PL> it_says_BALLS_on_your_forehead wrote:
>> Paul Lalli wrote:
>> > Uri Guttman wrote:

>> > > s/your/you're/. please spell your insults correctly if you want
>> > > them to have the maximum effect.
>> >
>> > While I do, of course, agree whole-heartily with your entire
>> > post, I can help but point out my amusement that someone who flat
>> > out refuses to use the shift key to correctly capitalize his
>> > sentences is complaining about someone else's spelling... ;-)
>>
>> you "can help" but choose not to, apparently ;-).

PL> You know... I went over that post 3 times before submitting,
PL> because I *really* wanted to avoid anyone calling me on my calling
PL> someone else on their calling someone else on grammatical and
PL> spelling mistakes.... and I completely missed an entire lack of
PL> "'t". Sheesh.

SERVERS YU WRITE FOUR TROLING ON MY LAK UF CAPITALIZATION! I DON'T LYKE
YOU'RE ATITOOD!

Heath

unread,
Feb 23, 2006, 1:23:12 PM2/23/06
to
Uri Guttman wrote:
> >>>>> "H" == Heath <hdew...@maxtc.com> writes:
>
> H> This is actually how I thought it would work, but until your
> H> hypothetical version of Perl is released I guess I'll have to:
>
> H> my @fields = split ($delim ? $delim : ' ');
>
> H> which is decidedly less pretty.
>
> since that only tests $delim for perl truth, you can just use:
>
> @fields = split ($delim || ' ');
>

Well, that made sense to me too. It turns out we were both wrong:

use strict;
use warnings;

$_ = "\t\t\tthis\t\tis\tsome\t\t\ttext\t\t\n";

my $delim = '';

print ($delim || "\n\$delim is false\n"); # Sanity check.
print "split - variable w/or\n";
print "[$_]\n" for (split ($delim || ' '));

print "split - variable w/ternary\n";
print "[$_]\n" for (split ($delim ? $delim : ' '));

# Impractical useage of || and ?: just for illustration.

print "\nsplit - literal w/or\n";
print "[$_]\n" for (split ('' || ' '));

print "split - literal w/ternary\n";
print "[$_]\n" for (split ('' ? '' : ' '));


======== OUTPUT ============

$delim is false
split - variable w/or
[ this is some
text
]
split - variable w/ternary
[ this is some
text
]

split - literal w/or
[this]
[is]
[some]
[text]
split - literal w/ternary
[this]
[is]
[some]
[text]

======= END OUTPUT ========

My first guess was that in order to get the special case [split ' '],
the call must appear exactly that way in the code (or as [split]).
My last 4 lines of code shatter that theory, so I'm at a loss. Maybe
it has something to do with the levels of interpretation. ie: because
one of the operands of the or/ternary is a variable, the expression
isn't interpreted as a literal. I know very little about perl
internals so I'm just guessing here.

>
> BTW, you should not make one followup to three different posts (and from
> three different posters). it only confuses the threads.
>
> uri
>

Sorry, thanks for the tip.

Aaron Baugher

unread,
Feb 23, 2006, 11:58:41 AM2/23/06
to
"Paul Lalli" <mri...@gmail.com> writes:

That's one of the immutable laws of nature: when posting a grammar or
spelling flame, you're guaranteed to make a similar error of your
own. Usually a worse one.

Heath

unread,
Feb 23, 2006, 2:33:58 PM2/23/06
to
A subtle yet accurate indicator of where this discussion
has gone:

==== my right border =====

Related Pages
Spelling errors galore
"Liason"? It should of course be "liaison". This mistake has
...
thestar.com.my

Who You Callin' Ungrammatical?
By Jan Freeman, the Boston Globe. WHOM IS disappearing from the ...
www.fcnp.com

perlfaq7 - Perl Language Issues
www.perl.com

==== END my right border =====

Listed in order of relevance.

Uri Guttman

unread,
Feb 23, 2006, 3:11:41 PM2/23/06
to
>>>>> "H" == Heath <heath_...@yahoo.com> writes:

H> Uri Guttman wrote:
>> >>>>> "H" == Heath <hdew...@maxtc.com> writes:

H> Well, that made sense to me too. It turns out we were both wrong:

H> # Impractical useage of || and ?: just for illustration.

H> print "\nsplit - literal w/or\n";
H> print "[$_]\n" for (split ('' || ' '));

H> print "split - literal w/ternary\n";
H> print "[$_]\n" for (split ('' ? '' : ' '));

those are compile time folded constant expressions. so split will see
' ' only. seems that split's signature checking is compile time for ' '
and not runtime in any way.

so the only solution is different calls to split. that can be done
easily with anon subs or basic conditionals.

Tad McClellan

unread,
Feb 23, 2006, 6:04:48 PM2/23/06
to
thrill5 <nos...@comcast.net> wrote:

> Wow! Two birds with one stone!!


It is much more likely "dozens" rather than "two".

it_says_BALLS_on_your forehead

unread,
Feb 23, 2006, 6:56:01 PM2/23/06
to

Tad McClellan wrote:
> thrill5 <nos...@comcast.net> wrote:
>
> > Wow! Two birds with one stone!!
>
>
> It is much more likely "dozens" rather than "two".

make that "baker's dozens" (sounds more ominous).

robic0

unread,
Feb 24, 2006, 8:52:59 PM2/24/06
to

I'm going to have to state this one more time:

OR it does just a HARDCODED replacement of the "split ' '" form!
Which is a hack patch of some feeble minded Perl programmer.

Where it does it i don't know. But i'll tell you one thing...
The PARAMETER is fuckin *not* passed as a literal to the
os subroutine as uri ghelik the spoon man mentioned!!

0 new messages