Alex Rivera | Logout

split() but keep delimiter

Asked 2013-02-16T06:47:41.360
18
 my $string1 = "Hi. My name is Vlad. It is snowy outside.";

 my @array = split('.' $string1); ##essentially I want this, but I want the period to be kept

I want to split this string at the ., but I want to keep the period. How can this be accomplished?

Edit
Report

1 Answer

13

If you don't mind the periods being split into their own elements in the array, you can use parentheses to tell split to keep them:

my @array = split(/(\.)/, $string);
answered 2013-02-16T06:53:58.357

Your Answer