use strict; use warnings; my $string = 'gregarious gregarious gregarious'; my $count = 0; $string =~ s/gregarious/$count++ ? "\\underline{gregarious}" : "\\footnote{gregarious}"/ieg