Several days ago in the CB, there was a discussion concerning the merits of 0 vs 1 based arrays. There was something that was said that bothered me - "if you are good, it doesn't matter if arrays are 0 or 1 based because you never need to use their index". I assume the person was referring to (pop, push, shift, unshift, [-1], $#, etc), which I use, but I was still offended. I may be new to Perl, but I have found instances where I need to use an index. For example, I have two different arrays that are related, but need to be kept separate. They are related by their indices. Consider the following:

#!/usr/bin/perl -w use strict; my @firstnames = qw(john jacob jingle heimer smitz); my @lastnames = qw(brown black gray greene purple); my @fullnames; for (0..$#firstnames) { push(@fullnames, $firstnames[$_] . " " . $lastnames[$_]); } print "$_\n" foreach (@fullnames);

I know this is a simplistic example, but it still requires using an index and knowing the base. I didn't say anything in the CB because I honestly felt that it wouldn't contribute to the 0 vs 1 discussion. It most likely would have spurred an unhealthy arguement.

My questions:

  • 1. Is my logic flawed or is there a way to avoid indices all together?
  • 2. While I was trying to grok this, I thought of a "feature" that I believe could also be useful. Say I wanted to find out if "heimer" existed in the array and if so, at what indices. How would I go about doing that. Of course the obvious way is loop over all the elements of the array checking each one against "heimer" and pushing the index to a new array, but is there a better way? (see below for a ficticious example of this "feature".

    my @values_to_check = indices(@firstnames , "heimer");
    As always, your insight and feedback is appreciated.

    Limbic~Region


    In reply to Array indices by Limbic~Region

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.