http://www.perlmonks.org?node_id=264562

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

Given two "parallel" arrays, @d and @z, known to be of equal size, want to append each element of @d onto each element of @z:

$z[$_] .= $d[$_] for 0..$#z; # one way @z = map($z[$_].$d[$_], 0..$#z); # another way

Better ways to do it? Will Perl 6 do this more elegantly?

Title edit by tye

Replies are listed 'Best First'.
Re: Appending one array onto another
by sauoq (Abbot) on Jun 10, 2003 at 09:00 UTC
    $_ .= shift @d for @z; # Elegant, but destroys @d. my $i; $_ .= $d[$i++] for @z; # Less elegant, no destruction.

    I really don't care for that second one. I'd probably prefer the first one you gave over it. But, I offer it in the name of TIMTOWTDI anyway.

    -sauoq
    "My two cents aren't worth a dime.";
    
      sub { $_ .= shift for @z }->(@d);
      *grin*

      Makeshifts last the longest.

Re: Appending one array onto another
by Anonymous Monk on Jun 10, 2003 at 10:50 UTC
    perl6 (hyper ops) (synatx may not be exactly this, but you get the idea):
    @z ^_= @d;
    or
    @appended = @z ^_ @d;
Re: Appending one array onto another
by Skeeve (Parson) on Jun 10, 2003 at 09:18 UTC
    And here are my 2 Euro-Cent
    @z=map{$_.$d[-1+push@d,shift@d]}@z;
    Update: One can use a side-effect and avoid the assignment of an array:
    map{$_.=$d[-1+push@d,shift@d]}@z;

      And using aliasing to our advantage but avoiding map in void context yields

      $_ .= $d[-1 + push @d, shift @d] for @z;
      I'm not convinced that it's at all better than the first one in the OP though. This is a bit obscure for no really good reason.

      -sauoq
      "My two cents aren't worth a dime.";
      
        I like that one!

        It's obscure, yes. But not for "no really good reason" but for avoiding an aditional variable. But now as I think of it ;-) Okay: It's no really good reasone. Maybe I should think about my taking part in perl-minigolf and obfuscation.

Re: Appending one array onto another
by TomDLux (Vicar) on Jun 10, 2003 at 14:35 UTC

    The hyper operator is more appropriate

    @z .^~ @a; # I believe _ has been replaced by ~ # as the new replacement for '.'

    but multi-variable loops work, too, though not as fast.

    my @new; for @z;@d -> $z;$d { push @new, $z ~ $d; }

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Appending one array onto another
by novitiate (Scribe) on Jun 10, 2003 at 14:52 UTC
    honestly, maybe i'm too in love with perl5, but i don't see what could be more elegant than your first example. it's a nice one liner that is not very obscure - maybe not the most optimized - but appropriate here.

    humbly,
    novitiate

    "...goodnight you princes of main(e)"    --The Cider House Rules
Re: Appending one array onto another
by denthijs (Acolyte) on Jun 10, 2003 at 09:27 UTC
    i'm prolly reading the question wrong (just woke up).
    but to append two arrays i usually just @new = (@one,@two)
    but im too vague to understand the example code atm.
    cheers