Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 browsing the Monastery: (17)
As of 2014-07-11 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (232 votes), past polls