Info320

Assignment 2

Peter Woodman
0127119
Info 320
E. Efthimidias
Assignment 2

I created a spot of Perl code (actually my first real use of perl, so it’s a bit ugly) to generate an inverse index in .csv format. The code follows:

invindex.pl

#!/usr/bin/env perl

my %post;
my %dict;
my @files;
$/ = ' ';

foreach $fname (@ARGV) {
    push @files, $fname;
    open(DOC, $fname);
    while (<DOC>) {
        $_ =~ s/\W/ /eg;
        $_ = lc($_);
        $post{"$_"} = ($post{"$_"} + 1);
        ${$dict{"$_"}}{"$fname"} =  ${$dict{"$_"}}{"$fname"} + 1;
    }
}

print "word,total count,";
print "$_," for @files;
print "\n";
while (($key, $value) = each(%post)) {
    print $key.",".$value;
    print ",".(${$dict{"$key"}}{"$_"}+0) for @files;
    print "\n";
}