NetWallah has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to print them in NAME, AGE pairs.my @names = ('Jack', 'Emma', 'Robert'); my @ages = (20, 19, 35);
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 +years old"} Internal error while running expression: *** Unexpected "[" expecting subroutine parameters, trait or block at <interactive> line 1, column 27
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 is $age ol +d"} Jack 20 is Emma 19 old Robert 35 is old 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) -> $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 !! 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) -> @_ {say " @_[0] is @_[1] years old +;"} Jack is 20 years old; Emma is 19 years old; Robert is 35 years old;
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
"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 | |
|
Re: perl6/pugs zip + for + subroutine params
by perlfan (Parson) on Oct 09, 2007 at 01:39 UTC |