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

I know $foo isnt an array, I was just using that as an example.

a better example might be:
for ($num=0; $num<=100; num++){ if (@text[$num] contains 'string'){ $cnt++; } } print "string was found on $cnt lines"

Replies are listed 'Best First'.
Re^3: find text in string
by sk (Curate) on Sep 13, 2005 at 03:45 UTC
    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.

Re^3: find text in string
by GrandFather (Saint) on Sep 13, 2005 at 03:43 UTC

    If you want good answers you need to ask good questions. The better you can specify the problem, the better we are able to answer it. You have now had two replies that sugested that you use the index function. Have you tried it?


    Perl is Huffman encoded by design.