in reply to Re^2: What's most efficient way to get list context? (honest)
in thread What's most efficient way to get list context?

As I understand it, the standard "list in scalar context" behavior is "size of the list".
You should note that your winner does not exhibit that behavior.
grep 1, @list;
would return a count (or the whole list, depending on context), but I suspect assigning to the empty list is more efficient. It's certainly a tad shorter to type.

Update: Had grep 0, due to scrambled thinking that it would still return a count of all elements, yet not build a list. Gah. Thanks, Pustular.


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: What's most efficient way to get list context? (honest)
by tlm (Prior) on Apr 15, 2005 at 14:47 UTC

    You should note that your winner does not exhibit that behavior.

    Well there are two issues here (and as tye already noted, they are only meaningful in unusual situations such as benchmarking): one is what to wrap around an expression to induce perl to evaluate it in list context, and the other one is what is the value that this whole ball of code will return. The "winner" in my original node happens to do more than simply return the list that was produced by the wrapped expression. I can imagine any number of "wrappers" that would succeed in inducing a list context in the evaluation of the wrapped expreesion, and yet return entirely disparate things. And this is kind of my point: it makes no sense to have to wrap extraneous code around an expression just to "tell" perl to evaluate it in list context.

    the lowliest monk

      it makes no sense to have to wrap extraneous code around an expression just to "tell" perl to evaluate it in list context
      Assignment to an empty list is an easy and sensible way to apply list context to a function. It's no more extraneous than a list keyword; it's just more idiomatic.

      There's even a kind of symmetry: you can't assign a scalar to undef, so you have a scalar keyword; you can assign a list to (), so you don't need a list keyword.


      Caution: Contents may have been coded under pressure.