perlquestion
Limbic~Region
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, <CODE>[-1]</CODE>, $#, 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:
<p>
<CODE>
#!/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);
</CODE>
<p>
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.
<p>
My questions:
<p>
<li> 1. Is my logic flawed or is there a way to avoid indices all together?
<li> 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".
<p>
<CODE>
my @values_to_check = indices(@firstnames , "heimer");
</CODE>
As always, your insight and feedback is appreciated.
<p>
[Limbic~Region]