jcpunk has asked for the wisdom of the Perl Monks concerning the following question:

I am playing around with split, and was wondering if there was a way to split on something and not have it be consumed by the operation.
for example, split('foo',split('w00tx0r',"bla foo w00tx0r moo")); and get
bla foo w00tx0r moo
I apologize if this has a fairly simple solution.....

jcpunk
Thanks for all the help that is was and will be

Replies are listed 'Best First'.
Re: split but not consumed
by davido (Cardinal) on Jul 23, 2004 at 04:35 UTC

    If you wrap in parens your split RE, you capture (as though it were one of the split results) whatever matched the split regexp.

    If you split using a lookahead assertion, nothing is gobbled up.

    These are two different approaches that yield different results, but both deserve mentioning. I will demonstrate both, but first, the normal, common, run-of-the-mill split:

    my $string = "This and that"; print "$_\n" for split /\s+and\s+/, $string; __OUTPUT__ This that

    That was the simple, and standard case. Now for the capturing method:

    my $string = "This and that"; print "$_\n" for split /\s+(and)\s+/, $string; __OUTPUT__ This and that

    And now for the lookahead assertion method:

    my $string = "This and that"; print "$_\n" for split /(?=\s+and\s+)/, $string; __OUTPUT__ This and that

    That third example is the trickiest. It basically matches (and thus splits) at the point at which the lookahead assertion begins, and because lookahead assertions are only assertions, they don't gobble anything at all, and thus, everything after the split-point is preserved as-is. Use care with this method; lookahead assertions are difficult to do right in split functions. Read the docs on split for details.

    Just for completeness, I want to demonstrate the lookahead assertion version with a split that has more than two resulting substrings as its outcome:

    my $string = "This and that and those too"; print "$_\n" for split /(?=\s+and\s+)/, $string; __OUTPUT__ This and that and those too

    ...that's pretty much what you were looking for, right? That's a great question, by the way.


    Dave

Re: split but not consumed
by broquaint (Abbot) on Jul 23, 2004 at 04:28 UTC
    Use a capture in your regex
    print "[$_]\n" for split /(foo)/, 'this foo is foo ... ey'; __output__ [this ] [foo] [ is ] [foo] [ ... ey]
    Also, split expects a delimiter then a string (and optionally a limit), so passing the result of one split to another can result in unexpected behaviour. See. the split docs for more info on its usage.
    HTH

    _________
    broquaint

Re: split but not consumed
by Zaxo (Archbishop) on Jul 23, 2004 at 04:28 UTC

    This doesn't work exactly as you imagined, but a captured pattern in split will be included in the resulting list.

    $_ = 'abcfoodefbarghibaz'; my @parts = split /(foo)/; print "@parts",$/ __END__ abc foo defbarghibaz

    After Compline,
    Zaxo

Re: split but not consumed
by ysth (Canon) on Jul 23, 2004 at 06:18 UTC
    davido answered this excellently, so I'd like to whine about a pet peeve: using a quoted string as the first arg to split. I would write that as split(/foo/,split(/w00tx0r/,"bla foo w00tx0r moo"));.

    Using "foo" instead of foo seems to create the false impression that split will work with either a regular expression or an exact string. Then you see things like split ".", which should really be split /\./.

    Split only takes a regex (with the confusing exception of " "), so show it that way.

      I laughed out loud when Abigail-II posted this response to a question of why $string =~ "stuff" works.

      Of course, with regards to split, a single quoted, single space, is a "special case."


      Dave

Re: split but not consumed
by PodMaster (Abbot) on Jul 23, 2004 at 05:35 UTC
    `perldoc -f split'
    ... If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter.
    split(/([,-])/, "1-10,20", 3);
    produces the list value
    (1, '-', 10, ',', 20)
    ...
    The manual never forgets :)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.