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;