|laziness, impatience, and hubris|
Re: The Scalar Range Operatorby denishowe (Acolyte)
|on Sep 11, 2012 at 13:43 UTC||Need Help??|
Can you use the scalar range operator with map and grep? Sure, but you have to be careful.
This does what you'd expect:
because grep's first argument is evaluated as a scalar.
This is very puzzling:
until you remember that map's first argument is evaluated as a list. What you meant, of course, was:
-- the expected result.
The puzzling case above gets even more puzzling if we show the list that map returns for each input:
we get the same ten elements as before, though in an unexpected way. The first line is evaluating:
Remember that this is being evaluated in list context (because we forgot the scalar()). So, our scalar range operator has accidentally become a list range operator. The final clue is that the list range operator only does string magic (e.g. ("a".."z")) if the strings are non-empty. Otherwise the arguments are converted to integers, in this case zeros:
which is just a list containing a single zero, as we saw above.
The other cases are now easy to understand.
is (1 .. ""), which is converted to (1 .. 0), which gives an empty list. Similarly,
is ("" .. 1) which gives (0, 1).