#This program gives a sorted frequency count of the forms in a file

print "What is your input file name:\n";
# Prints the text on the screen

chomp($infile=<STDIN>);
# Picks up the file name from the keyboard input

open IN, $infile or die "No file, no fun!";
# Opens the input file to read from it and assigns the previously given name to it

open OUT, ">$infile.fre" or die "No file, no fun!";
# Opens the output file giving it the same name as the input file has and the extension .fre

while (<IN>) {
# While there is something in the input file do the following

    s/[.,!;:?"']//g;
# Replace (s/) any of the characters within square brackets with nothing (//) globally (g)
# This is needed to avoid counting the same form twice - once with and once without a comma behind it

    @niska=split/\s+/,$_;
# Split the input file ($_) on one or more blakns or newlines (/\s+\) and puts it into the array called @niska

foreach $nisk (@niska) {
# for each record ($niska) in the array called @niska do the following

$count{$nisk}++;
# increse the number ($count) associated with the record ($nisk) 
# any time the same record is found

}
# finish counting records
}
# finish reading file and putting it into the array

foreach $nisk (sort {uc($a) cmp uc($b)} (keys %count)) {
# for each record in the array sorted case-insensitivelly 
# and its number of occurences

print OUT "$nisk $count{$nisk}\n";
# Print to the output file both the word ($nisk) and the 
# number of occurences ($count{$nisk}) adding a new line after each record

  }
# Finish printing to the output file once all records are done

close (IN) or die "D'oh!";
# Close the input file or close the program

close (OUT) or die "D'oh!";
# Close the output file or close the program