On Thu, Jan 15, 2009 at 12:46:09PM +0300, Richard Hainsworth wrote:
> Larry Wall wrote:
>> On Wed, Jan 14, 2009 at 09:55:38AM +0300, Richard Hainsworth wrote:
>>
>>> 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.
>>>
>>
>> Maybe I'm missing something, but you can always do:
>>
>> $pair.key.foo.fmt("%s"),
>> $pair.value.bar.fmt("%s")
>>
>> Larry
>>
> This surely requires that foo and bar are defined as sub's.
It was a notional action, just as the %s is a notional format.
The main point was that you can deref $pair twice.
> Suppose I just need a single transformation of a value before outputting
> it? Eg., expressing .value as a percentage of $count?
>
> Is there some way to include a one-off in the subroutine "chain"?
> Something like
> $pair.value.{shift / 10}.fmt("%s");
Well, I'm sure there's some way to do that, but certainly not
with any existing subscript notation.
Maybe it's better to just use parens:
($pair.value/10).fmt("%s")
Admittedly that doesn't interpolate well, which is part of the point
.fmt in the first place, and the way interpolation is defined to
end on brackets of some kind:
say "My value = $foo.fmt('%d')."
So I guess there's some small need for mapping a scalar value.
$pair.value.map({$_/10}).fmt("%s")
which maybe can even be written
$pair.value.map(*/10).fmt("%s")
Larry