Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: returning scalar context from map or grep

by BrowserUk (Patriarch)
on Apr 28, 2003 at 13:33 UTC ( [id://253680]=note: print w/replies, xml ) Need Help??


in reply to returning scalar context from map or grep

Now you know how to do it, you might think twice about whether you should do it?

Using map or grep to retrieve a single value is just as bad as using it in a void context I think. Your still building a (potentially large) list, only to discard all but one of the values. I do it myself on occasion, so I'm not waving a big stick, but it's worth thinking about the cost when you do it.

Actually, it may be even worse as I seem to recall that they had been 'fixed' to not bother building the return list in a void context? I could be wrong on that.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.
  • Comment on Re: returning scalar context from map or grep

Replies are listed 'Best First'.
Re: Re: returning scalar context from map or grep
by !unlike (Beadle) on Apr 28, 2003 at 13:59 UTC

    Thanks for the warning.

    However in this instance the problems you noted do not occur in my case. I'm using grep to return a list of hash keys are in the superset and not the subset of two hases. However I know that the difference in the size of the two hases will only ever be one or two key/value pairs.

    As I am only ever interested (for now) in the first element I wanted to return a scalar value so that later code didn't have to manage an array.

    If at a later date the size of the difference does increase I will reconsider my aproach to this problem.

    Cheers all.

    !unlike

    I write my Perl code like how I like my sex: fast and dirty. ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253680]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found