I have a question based on some examples in my very basic Perl book, which, I must admit, is "Perl for Dummies." (I know, I know, I'm going to buy the Llama as soon as my paycheck clears!)
I'm quoting here directly from the book:
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,
$TheRecord="Serling|Rod|Twilight Zone|";
@TheFields=split (/(\|)/, $TheRecord);
Perl returns
('Serling', '|', 'Rod', '|', 'Twilight Zone', '|')
You can supress your desire to have the delimiters returned by using the (?: extension to regular expressions. For example, given the following statements,
$TheRecord="Serling|Rod|Twilight Zone|";
@TheFields=split(/(?:\|)/, $TheRecord);
Perl returns
('Serling', 'Rod', 'Twilight Zone')
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 used
@TheFields=split(/\|/, $TheRecord);
Is there a difference I'm not perceiving? And if so, why would the longer-winded version be used?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.