Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: find text in string

by sk (Curate)
on Sep 13, 2005 at 03:45 UTC ( [id://491454]=note: print w/replies, xml ) Need Help??


in reply to Re^2: find text in string
in thread find text in string

I would probably re-write it a bit

untested

use strict; use warnings; open (IN, 'myfile.txt') or die $!; my @lines = <IN>; my $cnt = 0; for my $line (@lines) { if ($line =~ /somekeyword/) { $cnt++; } } print ("somekeyword was found in $cnt lines\n");

You could shorten this code a LOT but wanted to make it a little more explicit.

NOTE: You don't have to necessarily put the whole file in an array. You can use while(<IN> and check that way too.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-19 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found