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

Bretheren and Sisteren .. I'm trying to understand the "for <ZIP syntax> -> (param)" syntax in perl6, using pugs Version: 6.2.13, on Windows. I'm getting confusing results - I cannot tell if this is a pugs issue, or a perl6 syntax problem, documentation problem, or (most likely) my brain malfunction. Given:
my @names = ('Jack', 'Emma', 'Robert'); my @ages = (20, 19, 35);
I'm trying to print them in NAME, AGE pairs.
Here is my code with results from pugs:
pugs> for zip(@names; @ages) -> [ $name, $age ] {say " $name is $age +years old"} Internal error while running expression: *** Unexpected "[" expecting subroutine parameters, trait or block at <interactive> line 1, column 27
This (to me) seems like the documentation on the pugs website is wrong. So I try something different - remove the brackets:
pugs> for zip(@names; @ages) -> $name, $age {say " $name is $age ol +d"} Jack 20 is Emma 19 old Robert 35 is old undef
OK - that does run, but seems to mix up the 2 entities into a single variable, probably a array-ref.
pugs> for zip(@names; @ages) -> $name, $age {say " $name.[0] is $ +name[1] old; while $age.[0] may be $age [1]"} Jack is 20 old; while Emma may be 19 Robert is 35 old; while may be undef
OK - making SOME progress above - at least I extracted all the data. Still, it did not put info into the parameters in a manner I would like.
pugs> for zip(@names; @ages) -> @_ {say " @_[0] is @_[1] years old +;"} Jack is 20 years old; Emma is 19 years old; Robert is 35 years old;
OK !! That worked perfectly. But - I would like to use NAMED variables, not @_. None of the attempts below do what I want:
pugs> for zip(@names; @ages) -> ($n,$a) {say " $n is $a old; "} Jack 20 is Emma 19 old; Robert 35 is old; undef pugs> for @names Z @ages -> ($n,$a) {say " $n is $a old; "} Jack 20 is Emma 19 old; Robert 35 is old; undef pugs> for @names Z @ages -> ($n;$a) {say " $n is $a old; "} Jack 20 is Emma 19 old; Robert 35 is old; undef pugs> for @names Z @ages -> $n,$a {say " $n is $a old; "} Jack 20 is Emma 19 old; Robert 35 is old; undef pugs> for @names Z @ages -> $n;$a {say " $n is $a old; "} Jack 20 is Emma 19 old; Robert 35 is old; undef
Could a fellow monk point me in the right direction - where can I RTFM on syntax related to this, and how should I do this ?

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re: perl6/pugs zip + for + subroutine params
by moritz (Cardinal) on Oct 09, 2007 at 06:23 UTC
    Pugs doesn't implement slice and list contexts correctly at the moment.

    The idea is that if there is no explicit slice context (=nested array context), the lists are automatically flattened.

    So 1, 2, 3 Z  <a b c> should become 1, 'a', 2, 'b', 3, 'c' (unless in slice context). When you use this list in a for loop, you can supply a "pointy block" with two paramters:

    for @list -> $a, $b { say "$a: $b" }

    There's no way you can ever use [$a, $b] as a formal parameter since it's not an lvalue (we don't have pattern matching in sub calls like haskell has... at least not yet ;-)

    You can read more about the syntax at the Official Perl 6 Documentation.

Re: perl6/pugs zip + for + subroutine params
by perlfan (Parson) on Oct 09, 2007 at 01:39 UTC