#!/usr/bin/perl
# this program figures tri-grams for a file
# TODO:
# 1. input file on command line **DONE**
# 2. checking for case where length of file % 3 == 1 **DONE**
# 3. output to file **DONE**
use strict;
defined($ARGV[1]) or die "Usage: my.vignere.pl \n";
my ($buf, $out, $add, $length_of_file, $count, @in_file,
$key, $key_length, $key_loop);
$length_of_file = 0;
open(O,">".$ARGV[1]) or die "Output file
open failed: $!\n";
open(IN,"$ARGV[0]") || die "Sorry. Couldn't open: $!\n";
while (read (IN, $buf, 1)) {
if ($buf eq "\t")
{
$buf = "\n";
}
if ($buf ne "\n") {
$in_file[$length_of_file] = $buf;
$length_of_file++;
}
}
close (IN);
$length_of_file = $length_of_file -1;
print ("key: ");
$key = ;
chomp ($key);
$key_length = length($key);
$key_loop = 0;
for ($count = 0; $count <= $length_of_file; $count++) {
$buf = $in_file[$count];
$add = (substr($key,$key_loop,1));
$out = ((ord($buf) - ord($add)) - 32) % 94;
$key_loop = ($key_loop + 1) % $key_length;
print O (chr($out + 32));
if (($count != 0) && (($count % 70) == 0)) {
print O ("\n")
}
}
print("\n");
close(IN);
close(O);