my %ent= ( "&" => 38, "<" => 60, ">" => 62, #... ); $string = "IBM & Microsoft > Sun\n"; print $string; for ( keys %ent ) { $string =~ s/$_/&#$ent{$_};/g } print $string; #prints IBM & Microsoft > Sun