Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: mix arrays

by rjt (Curate)
on Aug 07, 2013 at 16:57 UTC ( [id://1048382]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048382]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-25 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found