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

Hello monks, I am using split on a string that contains multiple delimiters and split treats consecutive delimiters as one delimiter instead I want split to return empty array element for every delimiter it finds. Is this possible? Should I parse instead of split? For example:
# Input string is "ab''c" and ' is the delimiter for split. # After "ab" there are two delimiters, hence split returns # an array with 3 elements as show in the output below. # But I want output to have 4 elements instead of 3. One # empty array element for every delimiter split encounters my @arr1 = split("'", "ab''c"); print Dumper(@arr1); Output: $VAR1 = 'ab'; $VAR2 = ''; $VAR3 = 'c'; I want the output to be $VAR1 = 'ab'; $VAR2 = ''; $VAR3 = ''; $VAR4 = 'c';
Thanks

Update: Thanks to Jethro. My understanding of split was wrong, it returns an array with elements in between the delimiters. His suggestion of returning the array with delimiters, helped me resolve the issue I was dealing with.

Replies are listed 'Best First'.
Re: Split with consecutive delimeters
by jethro (Monsignor) on Nov 01, 2013 at 00:27 UTC

    Your words are contradicted by your example. "ab''c" has three elements delimited by 2 delimiters. $VAR2 in the output is not a placeholder for a delimiter, it represents what is between the two delimiters.

    If you want to have delimiters also represented in the output, you can use this:

    my @arr1 = split("(')", "ab''c");

    UDPATE: It seems you want '' to be handled as one delimiter, but translated into empty strings.

    my @arr1x = split("('+)", "ab''c"); my @arr1; foreach (@arr1x) { if (/'/) { push @arr1, ('') x length($_); } else { push @arr1, $_; } }

    UPDATE2: Arrgh, we are communicating out of sync ;-)

      Gotcha! Thanks for your reply. Your suggestion of split with delimiters in output is what I was looking for.
Re: Split with consecutive delimeters
by choroba (Cardinal) on Nov 01, 2013 at 00:16 UTC
    I do not understand your requirement. Using a different delimiter might be more instructive:
    ab | | c ^ | | only one empty member

    If you want to replace each empty element with two empty elements, just try

    map length($_) ? $_ : ($_, $_), split /:/, 'ab::c'
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ