These simple, common problems are good learning experiences.
Here's mine, from a discussion on OO in January 1996.
Notice that I used a totally different mechanism to prevent duplicate entries for one line.
sub input
{
while (<>) {
# parse the line into words
my %temp;
while (/\b\w+\b/g) {
++$temp{$&};
}
# add the found words to my global results list
my $key;
while (($key)=each(%temp)) {
push (@{$concord{$key}}, $.);
}
}
}
sub output
{
my @words= sort keys %concord;
my $key;
foreach $key (@words) {
# print "$key @{$concord{$key}}\n"
print ("$key ", join (', ', @{$concord{$key}}), "\n");
}
}
input;
output;