#!/usr/local/bin/perl -w 

if (@ARGV<1) {
    print "This program needs a <filename>\n";
    exit 1;
}

open IN, "<".$ARGV[0] or die "Cannot open $ARGV[0]: $!";
$count=0;
while (<IN>){
    $line = $_;
    next if ($line =~ /^\n/); 
    chomp $line;
    $line =~ s/\,//g;
    $line =~ s/\.//g;
    $line =~ s/\!//g;
    @words = split /\s+/, $line;
    $term[$count] = reverse $words[-1];
    $count++;
}
close IN;
# print "$count líneas\n";

$sum=0;
for $i (@term){
    for $j (@term){
	next if ( $i eq $j );
	for $k (0..length $i){
	    if (defined substr $j,$k,1){
		if (substr($i, $k, 1) eq substr($j, $k, 1)){
		    $sum++;
		}else{
		    last; 
		}
	    }else { last ; }
	}
    }
}
$sum /= $count*$count;
print "El índice de poeticidad es $sum\n";

__END__


