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

Discussion in 'Perl Misc' started by Sara, Apr 7, 2004.

  1. Sara

    Sara Guest

    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.
     
    Sara, Apr 7, 2004
    #1
    1. Advertising

  2. Sara

    Uri Guttman Guest

    >>>>> "S" == Sara <> writes:

    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
     
    Uri Guttman, Apr 7, 2004
    #2
    1. Advertising

  3. 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Apr 7, 2004
    #3
  4. Sara

    Paul Lalli Guest

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

    On Wed, 7 Apr 2004, Sara wrote:

    > 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
     
    Paul Lalli, Apr 7, 2004
    #4
  5. In article <>,
    (Sara) wrote:

    > 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

    --
    Pukku
     
    Richard Morse, Apr 7, 2004
    #5
  6. Sara

    Sara Guest

    "Tassilo v. Parseval" <> wrote in message news:<c51o1l$il5$-Aachen.DE>...
    > 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


    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
     
    Sara, Apr 8, 2004
    #6
  7. Re: Why does split operate over multiple lines in the absence of"ms" ?And why doesn't $_ work with split?

    Paul Lalli wrote:
    >
    > On Wed, 7 Apr 2004, Sara wrote:
    >
    > > 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.


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


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 12, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Condarco

    Working with Informix's SPL

    Boris Condarco, Aug 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    393
    Boris Condarco
    Aug 25, 2003
  2. Scott Abel
    Replies:
    0
    Views:
    448
    Scott Abel
    Sep 27, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  4. Ixiaus
    Replies:
    2
    Views:
    315
    Max Erickson
    Apr 27, 2008
  5. TechCrazy

    split long string over two lines

    TechCrazy, Jun 16, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    176
    TechCrazy
    Jun 16, 2005
Loading...

Share This Page