I think the short answer is simply "Don't use 1-based arrays". There are rare occasions where they're a more natural fit to your data, and so, in this case you can adapt accordingly. Even so, I would still hesitate.
One example is this:
my @months = qw[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ];
So, you can see that when accessing
$month[2] that 'Mar' is the result. It's possible to put in a dummy month, such as like this:
my @months = '',qw[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ];
This would work, however, stepping back from the problem, you'll note there's little reason to label January as 1 internally. Look at
localtime as an example of how it can be done. In that case, January is month 0.
There are many arguments against 1-based index systems, and I have to concur with most. I'd rather have consistent data structures with offset work done in the interface layer, than vice-versa. For example:
print "Today is in the month ",$month[$calendar_month-1],"\n";
Which is straightforward enough. Wrapped in a function, it becomes even less of an issue:
sub get_month_name
{
my ($calendar_month) = @_;
return $month[$calendar_month-1];
}
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.