ScarletRoxanne has asked for the wisdom of the Perl Monks concerning the following question:
where I just want:
Sorry, that's not an anagram pair
Other, more minor issues for the especially generous:
1. I know what the FALSE values are for Perl, but I can't get the script to print FALSE by, for example, setting a variable to '' or 0, etc. or saying return ''. Ideally, I wouldn't have to put "print TRUE/FALSE" in the script at all.
2. I put in the last elsif statement in the script to see if it would affect the printing twice problem. It didn't, and now I'm curious why my m// expression doesn't work. It's supposed to find pairs that are identical except that one has more whitespace than the other.
Here's the script! I'm sorry it's so long - again, the problem is at the very end with the final "else" statement. Many thanks!!!
#To Run: Type start.pl on the command line. #The script should prompt you to enter a word or phrase. #Once you've done that, it'll prompt you for another one. #Then you will be told if the two terms are anagrams or not. #!/usr/bin/perl -w use strict; #I have to use this to make STDIN work. IDK why. $|=1; #variables my $aWord; my $bWord; my $word; my $sortWord; my $sortWords; my @words; my %anaHash; print "\nReady to play the anagram game? Excellent.\n\nType your first + word or phrase, then hit Enter.\n\n"; $aWord = <STDIN>; chomp $aWord; print "\n\nThanks! Now type your second word or phrase and hit Enter.\ +n\n"; $bWord = <STDIN>; chomp $bWord; #This foreach loop performs the following tasks: #1. Pushes the two words from STDIN into an array (unsure if this is r +eally necessary) #2. lowercases everything and removes all characters except for letter +s & spaces #3. splits both words into characters, sorts them alphabetically, then + joins the sorted letters into a single "word" #4.pushes the array into a hash @words = ($bWord, $aWord); foreach $word (@words) { $word =~ tr/A-Z/a-z/; $word =~ s/[^a-z ]//ig; $sortWord = join '', sort(split(//, $word)); push @{$anaHash{$sortWord}}, $word; } #This foreach loop tries to determine if the word pairs are anagrams o +r not. foreach $sortWords (values %anaHash) { #"if you see the same word twice AND the input was two identical w +ords:" if (1 < @$sortWords && @$sortWords[0] eq @$sortWords[1]) { print "\n\nFALSE: Your phrases are identical!\n\n"; } #"if you see the same word twice AND the input was two different w +ords (i.e. a real anagram):" elsif (1 < @$sortWords && @$sortWords[0] ne @$sortWords[1]) { print "\n\nTRUE: @$sortWords[0] and @$sortWords[1] are anagrams!\n +\n"; } #this is a failed attempt to identify pairs that are identical exc +ept one has extra spaces. Right now, this fails and falls into the "e +lse" category below. elsif (@$sortWords[0] =~ m/ +@$sortWords[-1]/ || @$sortWords[-1] =~ m/ +@$sortWords[0]/) { print "\n\FALSE: @$sortWords[0] and @$sortWords[-1] are NOT anagra +ms. Spaces are characters, too!\n\n"; } #This is supposed to identify anything that's not an acronym. But +the output prints twice! It's maddening!!!! else { print "Sorry, that's not an anagram pair\n"; } }
restored content by Discipulus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Final Print Statement in Foreach Loop Prints Twice
by haukex (Archbishop) on Nov 11, 2018 at 08:44 UTC | |
|
Putting back the content of the OP: Final Print Statement in Foreach Loop Prints Twice
by Laurent_R (Canon) on Nov 11, 2018 at 12:50 UTC | |
|
Shorter script
by Laurent_R (Canon) on Nov 11, 2018 at 12:38 UTC | |
|
Re: Final Print Statement in Foreach Loop Prints Twice
by rnewsham (Curate) on Nov 11, 2018 at 08:34 UTC | |
|
Re: Final Print Statement in Foreach Loop Prints Twice
by jdporter (Paladin) on Nov 22, 2018 at 19:27 UTC |