in reply to find text in string

$foo is not an array, it is a scalar. You could use index to find 'bar' in $foo, but that doesn't sound like what you want. Perhaps you should post the rest of you code and some sample data so we can see what you are trying to do.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: find text in string
by Anonymous Monk on Sep 13, 2005 at 03:31 UTC
    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"
      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.

      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.