#!/usr/local/bin/perl -w use strict; my %letter; while () { print("$_"), next if /^\s*$/; # print and skip empty lines my $c = substr $_, 0, 1; # get first character if (! exists $letter{$c}) { # have we seen it before? $letter{$c}++; print ":$c:\n"; } print; } __DATA__ AA for apple A for apple BB for ball B for ball C for ....