Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: (tye)Re: playing with map

by greenFox (Vicar)
on Mar 10, 2001 at 15:37 UTC ( [id://63460]=note: print w/replies, xml ) Need Help??


in reply to (tye)Re: playing with map
in thread playing with map

So using grep to return something other than a subset of the original list is quite restricted and is probably an indication that you shouldn't be using grep. (:

I have been guilty of writing

do_something() if (grep /something/, @array);
is this a "wrongly" used grep? Should I be writing something like-
foreach (@array){ &do_something() and last if /something/; }
... the grep seems easier on the eye IMO. Is it OK if you know the data source is always going to be small?

Replies are listed 'Best First'.
(tye)Re2: playing with map
by tye (Sage) on Mar 11, 2001 at 00:09 UTC

    do_something() if (grep /something/, @array);

    Actually, that doesn't bother me much at all. It is probably slower in most cases than some alternatives, but I'd probably only go about optimizing for that if I strongly suspected that @array was going to contain a whole lot of entries, many of which match /something/. But it is probably faster for cases where @array is quite large and most of the time none of its entries match /something/ (since the grep solution doesn't have to "dispatch" as many opcodes), so "premature optimization" could cost you here. Though I won't swear to anything without seeing benchmarks for a specific problem on a specific platform.

    It would be neat if grep could tell that it is in a "Boolean context" and just give up as soon as it finds the first match. Unfortunately, I don't think Perl has context information other than 1) void, 2) scalar, 3) list, or 4) list assignment of size N, that is automatically tracked. I think such tracking is planned (perhaps waiting for Perl6 these days), so we may see such an automatic optimization in the future.

    And I guess I should update my blanket statement on grep to be "So using grep to return something other than a subset of the original list (or, in a scalar context, the number of elements in that subset) is quite restricted and may be an indication that you shouldn't be using grep." With your code often being one of the exceptions in my book. (:

            - tye (but my friends call me "Tye")
Re: Re: (tye)Re: playing with map
by mkmcconn (Chaplain) on Mar 10, 2001 at 16:26 UTC
    I'm not qualified to say that it's wrong. I will say that it doesn't seem like the right tool. Just for an example, consider the hazard of accidentally doing something like:
    my @b = grep { s/apples/oranges/ } @a;
    If what you meant was to have a @b basket of oranges, and an  @a basket of apples, you'll be quite unhappy to find that grep has eaten all of your apples.

    mkmcconn

Log In?
Username:
Password:

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

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

    No recent polls found