Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Appending strings from one array onto strings of another

by eyepopslikeamosquito (Canon)
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

Comment on Appending strings from one array onto strings of another
Download Code
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 Skeeve (Vicar) 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 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
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 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://264562]
Approved by benn
Front-paged by arthas
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-09-18 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (111 votes), past polls