Why does split operate over multiple lines in the absence of "ms" ? And why doesn't $_ work with spl

S

Sara

To substitute over multiple lines, one would need something like

s/CAT/DOG/msg;

But to SPLIT those same lines the "ms" is apparently "implied":

my @all_matches = split /CAT/, $_;
my @all_matches = split /CAT/ms, $_;

will both split the entire paragraph, not just line 1. Why? Why would
one assume that programmers would need to substitute only line 1, but
wold always need to split the whole paragraph. Doesn't make sense, and
appears to be inconsistent. The only rationale I see roughly alluded
to in Camel is that split is the "opposite of" join, so since join
obviously works over multiple array elements, to be a true inverse
then split would need to work over the entire scalar?

Also, along these same lines why isn't $_ taken as the default arg for
split, such that:

split /CAT/, $_;

would be identical to

split /CAT/; # throws an error

?

This cat is curious.
 
U

Uri Guttman

S> To substitute over multiple lines, one would need something like
S> s/CAT/DOG/msg;

neither /m nor /s does anything there. /s only changes . to match \n. /m
only changes ^ and $ to work inside strings next to \n and not at the
ends. you don't have any need for /m nor /s there.

S> But to SPLIT those same lines the "ms" is apparently "implied":

S> my @all_matches = split /CAT/, $_;
S> my @all_matches = split /CAT/ms, $_;

S> will both split the entire paragraph, not just line 1. Why? Why would
S> one assume that programmers would need to substitute only line 1, but
S> wold always need to split the whole paragraph. Doesn't make sense, and
S> appears to be inconsistent. The only rationale I see roughly alluded
S> to in Camel is that split is the "opposite of" join, so since join
S> obviously works over multiple array elements, to be a true inverse
S> then split would need to work over the entire scalar?

huh???

split splits its input string whether it is one line or many. you don't
understand /m and /s at all and you are saying things like 'lines' which
have nothing to do with split or s/// or m//.

S> Also, along these same lines why isn't $_ taken as the default arg for
S> split, such that:

S> split /CAT/, $_;

S> would be identical to

S> split /CAT/; # throws an error

show the complete code and error.

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split Splits a string into a list of strings and returns
that list. By default, empty leading fields are
preserved, and empty trailing ones are deleted.

In scalar context, returns the number of fields
found and splits into the @_ array. Use of split in
scalar context is deprecated, however, because it
clobbers your subroutine arguments.

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

does that answer your question?

uri
 
T

Tassilo v. Parseval

Also sprach Sara:
To substitute over multiple lines, one would need something like

s/CAT/DOG/msg;

No, one wouldn't. The only thing you need is /g, and that doesn't have
anything to do with strings spanning multiple lines. The /s modifier is
only meaningful when you have a '.' which otherwise doesn't match
newlines. /m changes the meaning of '^' and '$'.
But to SPLIT those same lines the "ms" is apparently "implied":

my @all_matches = split /CAT/, $_;
my @all_matches = split /CAT/ms, $_;

Same here. These two lines do the same thing because

/CAT/
/CAT/ms

are equivalent.
will both split the entire paragraph, not just line 1. Why? Why would
one assume that programmers would need to substitute only line 1, but
wold always need to split the whole paragraph.

Note that split() has an additional third argument. If you want to split
only once, then pass 2 as third argument.

So the only thing that split() implies is /g and that I find rather
plausible.

Tassilo
 
P

Paul Lalli

Also, along these same lines why isn't $_ taken as the default arg for
split, such that:

split /CAT/, $_;

would be identical to

split /CAT/; # throws an error

?

What version of Perl are you using? Even on 5.005, $_ *is* the default
argument for split. If you're getting an error doing
split /CAT/;
it's not because you didn't give split enough arguments.

Paul Lalli
 
R

Richard Morse

Also, along these same lines why isn't $_ taken as the default arg for
split, such that:

split /CAT/, $_;

would be identical to

split /CAT/; # throws an error

?

I'm not sure about your first question, but $_ is the default for split
to work on:

#!/usr/bin/perl
use strict;
use warnings;

$_ = <DATA>;
print join("\n", split(/v/)), "\n";

__END__
firstvsecondvthirdvfourthvfifthvsixth

prints:
first
second
third
fourth
fifth
sixth

I'm using Mac OS X and perl 5.8.1-RC3

HTH,
Ricky
 
S

Sara

Tassilo v. Parseval said:
Also sprach Sara:


No, one wouldn't. The only thing you need is /g, and that doesn't have
anything to do with strings spanning multiple lines. The /s modifier is
only meaningful when you have a '.' which otherwise doesn't match
newlines. /m changes the meaning of '^' and '$'.


Same here. These two lines do the same thing because

/CAT/
/CAT/ms

are equivalent.


Note that split() has an additional third argument. If you want to split
only once, then pass 2 as third argument.

So the only thing that split() implies is /g and that I find rather
plausible.

Tassilo

Hmm curiously, you're correct. I seem to have /ms all over my code now
for no good reason. Major misconception on my part, thanks for
clearing it up.


G
 
J

John W. Krahn

Paul said:
What version of Perl are you using? Even on 5.005, $_ *is* the default
argument for split. If you're getting an error doing
split /CAT/;
it's not because you didn't give split enough arguments.

Heck, even on Perl 1.0 $_ is the default. :)


John
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top