For parsing a delimited text format like the one you've described, use split. Even if the next person who has to maintain your code is a "regex expert", assuming they know perl they'll expect split to be used in cases like this.
As for your example, I find the regex example very confusing. It's certainly not suitable for obtaining anything except the second field, and requires that the second field be of a particular form. If you wanted all fields from a regex, you'd need something like:
/([^!]*)!([^!]*)!([^!]*)!([^!]*)!([^!]*)/
which is surely a lot less readable or maintainable than
split /!/
If you just want the numbers between INT= and the following !, you might do something more like:
my $int = ($rec =~ /!INT=([0-9]+)!/);
Note that doesn't guarantee that the value will come from the second field, but neither did your example. If you wanted to do that, you might use:
my $int = ($rec =~ /^[^!]*!INT=([0-9]+)!/);
One final note, you should be using print rather than printf in your examples.
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.