For some reason I was expecting that the syntax should look more like this:
for( @male Z @female )->[$m,$f] { # .... }

Close. Two things got into your way. The first is syntactic: you need a space between for and (. The reason is that (nearly) everything of the form identifier(...) is parsed as a subroutine call. That way keywords don't conflict with function names, making it more robust to introduce new syntax.

The second one is a bit more interesting. In a signature, [$m, $f] stands for a single parameter, which is taken as a list, and unpacked into the two variables. But @list1 Z @list2 returns a list of lists which flattens the sublists when you iterate over it. To stop that flattening, you can write

for (@male Z @female).tree ->[$m,$f] { # .... }

which indeed works in current Rakudo.

Also, how would you push a stringified concatenation onto a new array?

Several ways:

my @array; for @male Z @female -> $m, $f { push @array, "$m $f; # or @array.push: "$m $f"; # or @array.push: $m ~ ' ' ~ $f } # or my @array = (@male Z @female).map("$^a $^b");

Since Z can also be used as a meta operator, you can also get zip and concatenation in one go, using Z~:

my @array = @male Z~ @female; # without joining space

To get the space between the two operands, you can use one of these tricks:

# with the list repetition operator: @array = @male Z~ ' ' xx @male Z~ @female; # with cross instead of zip: @array = @male X~ ' ' Z~ @female;

All of these solutions work with current Rakudo.


In reply to Re^3: how to avoid using index of array by moritz
in thread how to avoid using index of array by Achilles_Sea

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.