in reply to Re^3: split and capture some of the separators
in thread split and capture some of the separators
I think that it does set undef for non-captured delimiters in a split regex that contains capturing parens. Look at this example:
my $string = qq/This&and+that/; my @segments = split /(&)|\+/, $string; print "$_\n" foreach @segments; __OUTPUT__ This & and Use of uninitialized value in (.) concatenation or string at mytest.pl + line 4. that
In that example, you can see that the non-capturing portion of the match results in undef being plopped into the list element pertaining to that portion of the split.
As for documentation, the POD for split says, "If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter."
This is correct. It appears to be true that additional elements are created for each matching substring in the delimiter if the PATTERN contains parenthesis. But what it doesn't tell you is that though elements are created for each matching substring, those elements are only populated with a value if the corresponding portion of the PATTERN also uses capturing parens. If the specific portion of PATTERN that matched isn't captured with parens, the element is still created (since parens were used somewhere else within PATTERN), but the element isn't populated.
In this case, I would consider this a bug, either in the documentation (for not documenting what happens if you combine both capturing and noncapturing components in the split PATTERN), or a bug in Perl's split, for not quite accomplishing DWIMery.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: split and capture some of the separators
by BrowserUk (Patriarch) on Oct 08, 2004 at 02:35 UTC | |
by Anonymous Monk on Oct 08, 2004 at 17:59 UTC | |
|
Re^5: split and capture some of the separators
by Sandy (Curate) on Oct 08, 2004 at 15:20 UTC | |
|
Re^5: split and capture some of the separators
by ihb (Deacon) on Oct 11, 2004 at 02:17 UTC |