#!/usr/bin/env perl -w #Script: ord_frequency.pl #Frequency of ASCII characters in file or standard input #this can read from stdin or from a passed filename #Reads file one character at a time in binary mode #gets frequency of the ordinal value of each character. #Display frequency distribution of ordinal values. #---------------------------------------------------------- $file = shift; #optional file $file = '-' unless defined $file; #use STDIN if no file given open FILE, "$file" or die $!; #Open file my %freq; binmode FILE; #set binary mode my ($buf, $data, $n); while (($n = read FILE, $data, 1) != 0) { #read 1 char at a time $freq{ord($data)}++; #record ordinal frequency } close(FILE); #Display frequency Sorted Numerically by ordinal value printf("%12i: %s\n", $freq{$_}, $_) foreach sort { $a <=> $b; } keys %freq;
Enjoy!