You didn't mention what you want marriage() to do, but it probably doesn't really matter. Here's an example of how you can do it without indices, using List::MoreUtils pairwise function.

It looks a lot like map, except that it takes two arrays, not one, and it aliases each pair of elements as $a and $b instead of $_. Its prototype requires that you pass it two arrays, whereas map is happy to receive a list not specifically held in an array.

Here's some example code where we've set up marriage() to just 'join' the elements together with a single space character binding them. You could easily modify it to return an arrayref, or even do away with it entirely and do all your work inside the pairwise{...} block.

use strict; use warnings; use List::MoreUtils qw/pairwise/; my @rray1 = qw/ boy boymonkey boydog /; my @rray2 = qw/ girl girlmonkey girldog /; our( $a, $b ); my @married = pairwise{ marriage( $a, $b ) } @rray1, @rray2; print "$_\n" for @married; sub marriage { return join ' ', @_; }

The output will be:

boy girl boymonkey girlmonkey boydog girldog

If you have warnings enabled, it's necessary to either say "our( $a, $b );", or no warnings qw/once/; (within the smallest scope possible scope, such as inside of the pairwise{...} block), or some other means of satisfying the "used only once" warning for $a and $b.

While pairwise{} is a convenience, I really don't see any big advantage over this:

my @married = map{ marriage( $rray1[$_], $rray2[$_] ) } 0 .. $#rray1;

That snippet does iterate over the index which some nut may say is bad form, but on the other hand, it eliminates the need to pull in a CPAN module just for one line of sugar. The map{} approach even eliminates the need to worry about the "used only once" warning.


Dave


In reply to Re: how to avoid using index of array by davido
in thread how to avoid using index of array by Achilles_Sea

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.