Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Appending strings from one array onto strings of another

by eyepopslikeamosquito (Archbishop)
on Jun 10, 2003 at 07:51 UTC ( [id://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.

    -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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found