A coworker ran into a problem parsing some data, here's an example:
$_="|" x 5;
@foo = split /\|/,$_;
print scalar @foo; # prints 0
he expected @foo to have 6 elements. According to the perl documentation:
Splits a string into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted.
So if there are no fields to be parsed by the split, then it treats them all as empty trailing fields. My coworker said, "I think of them as empty leading fields!" The solution is to split as such:
@foo = split /\|/,$_,-1
to set an arbitrarily large limit and force split to treat them as empty leading fields, but that doesn't seem like the right (read: perl) way for things to happen. I'd expect perl to gracefully treat complete pseudo-emptiness as something, rather than nothing.
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.