# exercise 4.1 by erikt(at)science.uva.nl SOFTWARE # read the first phrase print "Please enter the first phrase: "; $phrase1 = ; if (not defined $phrase1) { $phrase1 = ""; } chomp($phrase1); # repeat until the first phrase is empty while ($phrase1 ne "") { # read the second phrase print "Please enter the second phrase: "; $phrase2 = ; if (not defined $phrase2) { $phrase2 = ""; } chomp($phrase2); # remove white space $phrase1 =~ s/\s+//g; $phrase2 =~ s/\s+//g; # convert upper case to lower case $phrase1 =~ tr/A-Z/a-z/; $phrase2 =~ tr/A-Z/a-z/; # extract characters and put them in the same order by sorting them @chars1 = split(//,$phrase1); $chars1 = join("",sort(@chars1)); @chars2 = split(//,$phrase2); $chars2 = join("",sort(@chars2)); # compare phrases if ($phrase1 eq $phrase2) { print "These phrases are identical\n"; } elsif ($chars1 eq $chars2) { print "These phrases are anagrams\n"; } else { print "These phrases are not related\n"; } # read the first phrase print "Please enter the first phrase: "; $phrase1 = ; if (not defined $phrase1) { $phrase1 = ""; } chomp($phrase1); } # final message print "Thank you for using this program!\n"; TEST erikt@stuwww:~$ perl -w 41.pl Please enter the first phrase: test Please enter the second phrase: TEST These phrases are identical Please enter the first phrase: d o r m i t o r y Please enter the second phrase: dirty room These phrases are anagrams Please enter the first phrase: caramba Please enter the second phrase: mascara These phrases are not related Please enter the first phrase: Thank you for using this program!