in reply to Possible to use "split", but save the delimiter?

Yes, it is possible, and it is even documented in split:
If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter.
1. split(/([,-])/, "1-10,20", 3);
produces the list value
1. (1, '-', 10, ',', 20)

Replies are listed 'Best First'.
Re^2: Possible to use "split", but save the delimiter?
by ultranerds (Hermit) on Feb 08, 2010 at 15:11 UTC
    Thanks, will give those a go :)

    Cheers!

    Andy
Re^2: Possible to use "split", but save the delimiter?
by ultranerds (Hermit) on Feb 10, 2010 at 16:50 UTC
    Hi,

    This almost works:

    my $test = qq|1-10,20 some.thing [[else]],bla bla bla|; my @temp = split /([,-\s\.\]\[]+)/, $test; use Data::Dumper; print Dumper(@temp);


    ..but for some reason, it doesn't seem to like the ]] bit :

    1 - 10 , 20 some . thing [[ else ]], bla bla bla


    We need it to split at:

    [[ ]] , - \s .


    Any suggestions?

    TIA!

    Andy

      This what you're after?

      $s = qq|1-10,20 some.thing [[else]],bla bla bla|;; print "'$_'" for split /([-, .]|\[\[|\]\])/, $s;; '1' '-' '10' ',' '20' ' ' 'some' '.' 'thing' ' ' '' '[[' 'else' ']]' '' ',' 'bla' ' ' 'bla' ' ' 'bla'

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thanks - works a charm <G>