With the great support in Perl for deep structures using hash/array references, you can avoid situations where data is split between two structures:
my @names = ( { first => 'john', last => 'brown' }, { first => 'jacob', last => 'black' }, # you get the point ); my @fullnames; for (@names) { push @fullnames, $_->{first} . ' ' . $_->{last}; }
I realize your example is just an example, and that it probably represents a much more complicated real-world coding situation. But the point is, when related items are indexed the same way, why not let them share the same space in an array using a hash ref?

However, this is just how *I* would have solved this particular problem. If I were you, I wouldn't get too scared of using array indices. I for one won't look down upon you for it ;) There is nothing *wrong* with using them, and there are certainly times when their use is merited -- while filling a complex structure in this node, using indices seemed to be the optimal solution. Sometimes having related data in the same structure is more intuitive, and at other times it might make more sense (usually for efficiency) to split them into separate arrays and use an index to iterate them both.

As for your second question, I know there will be a new operator that will allow you to check for inclusion of a scalar in an array. I believe it's called ~= (someone please correct me if I'm off here):

print "found heimer\n" if ("heimer" ~= @firstnames); # Perl 6 (or app +roximation)
Or maybe the syntax is the other way around. I don't know if the return value of that operator will be the item's index in the array, or just a true/false. Other monks here will certainly know the answer better than I.

blokhead


In reply to Re: Array indices by blokhead
in thread 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.