As BrowserUk and Athanasius have both shown, the best way to deal with the problem (in split and in regex matching in general) is not to have unmatched capture groups, which return undef, in the first place.
Sometimes, you're just gonna have capture groups that don't and won't match. The Perl version 5.10 Extended Patterns (?|pattern) "branch reset" operator is very handy for this:
If you don't have access to 5.10 or in general need to remove undefined (or zero-length, or whatever...) elements from a list, there's always grep:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $eqn = '$profit=$sales-$cogs'; my @wrds = split /(?|(=)|(-))/, $eqn; dd \@wrds; " ["\$profit", "=", "\$sales", "-", "\$cogs"]
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $eqn = '$profit=$sales-$cogs'; my @wrds = grep defined, split /(=)|(-)/, $eqn; dd \@wrds; " ["\$profit", "=", "\$sales", "-", "\$cogs"]
Update: Revised wording; meaning unchanged.
Give a man a fish: <%-{-{-{-<
In reply to Re: keeping split deliimiters causing empty string elements
by AnomalousMonk
in thread keeping split deliimiters causing empty string elements
by previous
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |