Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Appending strings from one array onto strings of another

by eyepopslikeamosquito (Chancellor)
on Jun 10, 2003 at 07:51 UTC ( #264562=perlquestion: print w/replies, xml ) Need Help??
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.

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

      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;
    @appended = @z ^_ @d;
Re: Appending one array onto another
by Skeeve (Vicar) on Jun 10, 2003 at 09:18 UTC
    And here are my 2 Euro-Cent
    Update: One can use a side-effect and avoid the assignment of an array:

      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.

      "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; }


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.


    "...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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://264562]
Approved by benn
Front-paged by arthas
[Lotus1]: I was here

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-06-18 17:57 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.