use strict; my $word = 'and'; while () { if (index($_, $word) > -1) { while (m/(?:^|[^a-zA-Z'])$word[^a-zA-Z']/gi) { print "The word\n$word\noccured in line " . ($.-1) . " as the " . ends(scalar(split(/[^a-zA-Z']+/, substr($_, 0, pos($_) - 1)))) . " word.\n\n"; } } } BEGIN { my @ends = ('','st','nd','rd'); sub ends { return $_[0] . @ends[$_[0] % 10] if $_[0] % 10 < 4 && $_[0] % 10 > 0 && int($_[0] / 10) != 1; return $_[0] . 'th'; } } __DATA__ Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that "all men are created equal" Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We have come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow, this ground -- The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here.