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];
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|