amarceluk has asked for the wisdom of the Perl Monks concerning the following question:
The split function does not normally return the delimiters in list items. You can use parentheses in your regular expression and split returns whatever is in the parentheses as items in the list....For example, when you run the following program, in which the | delimiter is enclosed in parentheses,It seems to me that the second program is just a longer-winded way to return exactly the same results as you'd get if you usedPerl returns$TheRecord="Serling|Rod|Twilight Zone|"; @TheFields=split (/(\|)/, $TheRecord);You can supress your desire to have the delimiters returned by using the (?: extension to regular expressions. For example, given the following statements,('Serling', '|', 'Rod', '|', 'Twilight Zone', '|')Perl returns$TheRecord="Serling|Rod|Twilight Zone|"; @TheFields=split(/(?:\|)/, $TheRecord);('Serling', 'Rod', 'Twilight Zone')
Is there a difference I'm not perceiving? And if so, why would the longer-winded version be used?@TheFields=split(/\|/, $TheRecord);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using split delimiters
by krujos (Curate) on May 23, 2002 at 21:36 UTC | |
by Aristotle (Chancellor) on May 24, 2002 at 05:29 UTC | |
by Molt (Chaplain) on May 24, 2002 at 10:56 UTC | |
by Aristotle (Chancellor) on May 24, 2002 at 15:05 UTC | |
|
Re: using split delimiters
by merlyn (Sage) on May 23, 2002 at 16:35 UTC | |
|
Re: using split delimiters
by amarceluk (Beadle) on May 23, 2002 at 16:52 UTC |