#!/usr/bin/perl use strict; my $old_string = "the dog chased my cat."; my ($new_string, $found_word); my %dict; while( ) { my ($key, $val) = $_ =~ /^(\d)\s(\w+)/; $dict{$val} = $key; } chop( $old_string ); my @words = split( " ", $old_string); foreach my $i ( @words ) { foreach my $j ( keys %dict ) { if ( $j eq $i ) { $new_string = $new_string . "$dict{$j} "; $found_word++; } } $new_string = $new_string . "$i " if ($found_word == 0); $found_word = 0; } chop( $new_string ); print "$new_string."; __DATA__ 1 dog 2 cat 3 chased 4 the