Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: how to add more intelligence to grep functionality

by GrandFather (Saint)
on Jan 11, 2006 at 19:42 UTC ( [id://522550]=note: print w/replies, xml ) Need Help??


in reply to how to add more intelligence to grep functionality

You have your answer, but for future reference your node would have been better if you trimmed the number of items in the array to show just enough cases to make your problem clear, and if your question matched the actual example in the code.

As it stands most browsers will either break your nasty long line in a random place, or not break it at all meaning that a huge amount of scrolling is required to read your node and its replies. Even replying in such a case is a real pain in the butt because the edit box is extreamly wide.

Cleaned up (and fixed) code could look like this:

use strict; use warnings; my @array = ('account_trend', 'trend_report', 'revenuebytrafficker'); my $value = qr'reve.*ker'; foreach (@array) { print "Matched :$_:\n" if /$value/; }

Prints:

Matched :revenuebytrafficker:

It's not clear to me why you had a grep in there. A version using grep is:

use strict; use warnings; my @array = qw(trend_report revenuebytrafficker revengebysmoker); my $value = qr'reve.*ker'; my @matches = grep {/$value/} @array; print ("Matched :" . (join ": :", @matches) . ":\n") if @matches;

Prints:

Matched :revenuebytrafficker: :revengebysmoker:

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: how to add more intelligence to grep functionality
by bobf (Monsignor) on Jan 12, 2006 at 03:37 UTC

    GrandFather gave you an example of how to make your code work, but I think a reference is in order (no, not that kind).

    Instead of my $value = 'reve*ker';, as in your original code, GrandFather changed it to my $value = qr'reve.*ker';. See that little qr in there? That's the quote regex operator. It takes a string, compiles it into a regex, and returns it for later use. To quote perlop,

    This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in m/PATTERN/. If "'" is used as the delimiter, no interpolation is done. Returns a Perl value which may be used instead of the corresponding /STRING/imosx expression.

    perlop. Lots of good stuff in there.

    HTH

    BTW, I'd like to second GrandFather's comment about long lines. Horizontal scrolling is awkward.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found