cat word.pl
#!/usr/bin/perl
my %count = ();
while (<>) {
@words = split(' ');
my %count = ();
foreach $word (@words) {
$count{$word}++ if($word =~ /sachin/);
}
}
foreach $word (sort keys %count) {
print "$word : occured $count{$word} number of times\n";
}
Note: <> - the daimond operator reads all the command line arguments (file by file) and split each line into words seperated by space.I have taken a hash to collect the words as my keys and it's num.of occurrence as value and incremented only if it matches word "sachin"
execute : perl word.pl cricketers.dat
output : sachin : occured 10 number of times
No comments:
Post a Comment