in reply to mix arrays

I would initially put a mapping from @b to @a into a translation mapping hash:

    my %b_a_map = map { (split / /)[0] => $_ } @a;

And then generating @c becomes a simple O(n) loop:

     my @c = map { $b_a_map{$_} } @b;

@c will now look something like:

apple 2 apple 2 apple 2 orange 5 orange 5 pear 3

Edit: Output re-run with verbatim OP input, instead of my slightly modified test input.

use strict; use warnings; omitted for brevity.

Replies are listed 'Best First'.
Re^2: mix arrays
by AnomalousMonk (Archbishop) on Aug 07, 2013 at 18:04 UTC

    The proposed contents of the  @c array doesn't seem to match the contents of the OPed specification exemplar.

      Indeed I had a few more lines in my sample output. I had extrapolated on the OP's `…' in the input arrays with the intent to produce more illustrative output, but that was obviously confusing, especially given my choice to omit their initialization in my example. :-) Fixed.