in reply to how to avoid using index of array

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