Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: how to quickly tell if number is in an array

by GrandFather (Saint)
on Oct 12, 2005 at 01:27 UTC ( [id://499337]=note: print w/replies, xml ) Need Help??


in reply to Re^2: how to quickly tell if number is in an array
in thread how to quickly tell if number is in an array

A good reply pg, and valuable in pointing out that "simplest" is not always "best" for given values of simplest and best. (In fact simplest is often "worst".)

OP did however ask about simplest and expressed interest in how grep could be used. Now if you could benchmark "simplest" that would be a neat trick.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^4: how to quickly tell if number is in an array
by sauoq (Abbot) on Oct 12, 2005 at 02:15 UTC
    Now if you could benchmark "simplest" that would be a neat trick.

    Well, one thing we might ask is which of two ways of doing something results in more work for the same result. In this case, pg's method produces less work because grep has to go through the entire list each time (and then, in scalar context, returns the number of times it found a match) whereas pg explicity returns when he finds the first occurrence. The are both linear, but grep will take about twice the time on average.

    For this particular problem, a for loop is probably the Right Way To Do It&trade. Sure, grep can be used as could map, a C-style for, a while or until, or a couple well-placed goto statements with corresponding labels... but, that doesn't mean they should be. I do agree that, as the OP asked about it, the use of grep should be discussed and, ultimately, the reasons to avoid it should be explained. (An explanation goes a lot further than a benchmark.) But, I don't like showing him how to use grep with no explanation of why it isn't ideal because he's likely to be drawn to the simplicity of the idiom without considering its drawbacks.

    -sauoq
    "My two cents aren't worth a dime.";
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2024-04-23 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found