hulketa has asked for the wisdom of the Perl Monks concerning the following question:

Hi perlmonkers, your help other times have benn so useful so here I go with another problem: I have two arrays like these: @a = apple 2, orange 5, pear 3... @b = apple, apple, apple, orange, orange, pear... So, I need to get a new array (@c) with @a items , but repeated so many time as they are repeated in @b. Like this: @c = apple 2, apple 2, apple 2, orange 5, orange 5, pear 3... so, I've tried something like this:

foreach (@b){ for $item (@a){ push(@c, $item) if grep {$item =~ m/$_/} @b;}} print "@c";

But it doesn't work, and it don't give me any error. It just don't run. If someone can help me I'd thanks so much! I'm obfuscate right now! Thanks guys!

Replies are listed 'Best First'.
Re: mix arrays
by 2teez (Vicar) on Aug 07, 2013 at 17:07 UTC

    You can do this:

    use Data::Dumper; my @a_array = ('apple 2', 'orange 5', 'pear 3',); my @b_array = ('apple', 'apple', 'apple', 'orange', 'orange', 'pear', +); my @c_array; for my $item_1 (@b_array){ for my $item_2 (@a_array){ push @c_array, $item_2 if $item_2=~/^$item_1/; } } print Dumper \@c_array;
    Produces ...
    $VAR1 = [ 'apple 2', 'apple 2', 'apple 2', 'orange 5', 'orange 5', 'pear 3' ];

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: mix arrays
by AnomalousMonk (Archbishop) on Aug 07, 2013 at 17:59 UTC

    Another small variation:

    >perl -wMstrict -le "my @a = ('apple 2', 'orange 5', 'pear 3'); my @b = qw(apple apple apple orange orange pear); ;; my %fruits; ++$fruits{$_} for @b; ;; my @c; push @c, ($_) x $fruits{ (split)[0] } for @a; printf qq{'$_' } for @c; " 'apple 2' 'apple 2' 'apple 2' 'orange 5' 'orange 5' 'pear 3'
Re: mix arrays
by McA (Priest) on Aug 07, 2013 at 17:00 UTC

    Hi

    my approach, as I don't know which rules are followed by the names 'orange 5':

    use strict; use warnings; use 5.010; my @a = ('apple 2', 'orange 5', 'pear 3'); my @b = qw/apple apple apple orange orange pear apple orange orange/; my %a; my @c; foreach my $fruit (@b) { if(exists $a{$fruit}) { push @c, $a{$fruit}; next; } my ($hit) = grep { $_ =~ /$fruit/ } @a; push @c, $hit; $a{$fruit} = $hit; } say join "\n", @c;

    Best regards
    McA

Re: mix arrays
by rjt (Curate) on Aug 07, 2013 at 16:57 UTC

    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.

      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.