Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Questions about Grep

by friedo (Prior)
on May 29, 2008 at 19:02 UTC ( #689068=note: print w/ replies, xml ) Need Help??


in reply to Questions about Grep

If you want to grep a file in Perl, you can read it into an array first:

use strict; open my $fh, "file.txt" or die "Error opening file.txt: $!"; my @lines = <$fh>; my @matches = grep /pattern/, @lines;
There's also the File::Grep module from CPAN which provides some easy-to-use file grepping functionality.


Comment on Re: Questions about Grep
Download Code
Re^2: Questions about Grep
by pc88mxer (Vicar) on May 29, 2008 at 19:14 UTC
    Don't know if this is any more efficient, but it's a few less keystrokes:
    use strict; open my $fh, "file.txt" or die "Error opening file.txt: $!"; print grep /pattern/, <$fh>;
Re^2: Questions about Grep
by Anonymous Monk on May 29, 2008 at 19:23 UTC
    Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2015-07-06 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls