#!/usr/bin/perl

# This script takes a word and a filename as arguments and prints the number of times 
# the word occurs in the file.

if(@ARGV != 2){
    die("Usage : getfreq <word> <filename> \n");
}

open(fp,$ARGV[1]);
$line=<fp>;

$count=0;                               # Store the frequency in variable "$count"
while ($line ne ""){                    # For each line, do 
    
    @words=split(/[\s+,\.]/, $line);
    
    for($i=0;$i<@words;$i++){           # For each word in the line, do
	
	if($words[$i] eq $ARGV[0]){     # If the words match, increment count
	    $count++;
	}
    }
    
    $line=<fp>;
}

print ("The word \"$ARGV[0]\" occurs $count times\n");
