Richard (>), Carl (>>), Andy (>>>):
>>> P6 treats the key/value as an anonymous 'pair' object so @ranking is an
>>> list of pairs. That's why:
>>>  say @ranking.pop.fmt("$m Medal: %s, %s")
>>>
>>> or, less succinctly:
>>>  say (pop @ranking).fmt("$m Medal: %s, %s");
>>>
>>> works - the pair object, popped off into the 'printf' like 'fmt' method
>>> (akin to:
>>> my $skater_pair = pop @ranking;
>>> printf("$m Medal: %s, %s\n", $skater_pair);
>>>
>>> or, longer:
>>> printf("$m Medal: %s, %s\n", $skater_pair.key, $skater_pair.value);
>>>
>>> ) devolves into a list (key, value) which fills the 2 '%s' fields.
>>>
>>> hashes, then, are lists of pairs, indexed by their 'key' attribute.
>>>
>>> I think, anyway.
>>>
>>
>> You are right about everything but the 'devolves into a list' part.
>> Look at S29:1599, and you'll note that one of the four flavours of
>> .fmt method lives in the Pair class. No devolving required.
>>
>> Because of this, the .fmt call is not equivalent to the version of
>> printf taking a pair:
>>
>>  printf("$m Medal: %s, %s\n", $skater_pair);
>>
>> In fact, this line throws a "Null PMC access in get_string()" in
>> Rakudo, which bug I just reported.
>
> Actually Andy had 'printf("...",$skater_pair.key, $skater_pair.value);'
> which works.

Yes, but I had the distinct impression from his message that he
considered the expanded version with C<$skater_pair.key,
$skater_pair.value> in the argument list equivalent to the form with
just C<$skater_pair> in the argument list. That is not the case in
Perl 6; arguments are not flattened so that they take up more
positions in the argument list than they appear.

> However, I came across one thing in solution #3 that I posted yesterday.
> $pair.fmt("%s %s") is nice, but it doesnt allow for any action on either
> value or key before printing (I wanted to print the value as a percentage),
> and this had to be done in a printf statement. In fact the printf statement
> was the longest and ugliest statement in the program.

Use .map?

// Carl

Reply via email to