in reply to find a string and count of its occurence in a text file

This would do the trick
use strict; use warnings; sub retriver(); my @lines; my $lines_ref; my $count; $lines_ref=retriver(); @lines=@$lines_ref; $count=@lines; print "Count :$count\nLines\n"; print join "\n",@lines; sub retriver() { my $file='source_data\data.txt'; open FILE, $file or die "FILE $file NOT FOUND - $!\n"; my @contents=<FILE>; my @filtered=grep(/abc:/,@contents); return \@filtered; }

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re^2: find a string and count of its occurence in a text file
by new@perl (Initiate) on Nov 14, 2007 at 05:59 UTC
    Thanks a lot narainhere :-) i need some explanation though, if you please.

    what is the function of -->
    sub retriver() in line number 3;

      It's function prototyping.That's needed because retriver() is not defined while it's been called.If you remove the prototyping (line 3) you have to use &retriver() while calling the function ,which tells the compiler to look for the definition somewhere below.

      The world is so big for any individual to conquer

        It isn't needed if you don't use prototypes