Home: Need a Custom or Prewritten Perl Program?: Throw Down The Gauntlet:
Anagram Generator



hunter
New User

Jan 23, 2003, 5:07 PM


Views: 67626
Anagram Generator

Hi,

I am looking for an anagram generator similar to, http://www.wordsmith.org/anagram/index.html .


hunter
New User

Jan 25, 2003, 5:10 AM


Views: 67615
Re: [hunter] Anagram Generator

Seems like know one wants to tackle this one. I have located part of a script that may work, http://perlmonks.thepen.com/135345.html . Perl coding is something I can't do. Can anyone work with the "monks" code and build a script that will handle phrases?


mike.berding
New User

Apr 23, 2011, 7:04 PM


Views: 61416
Re: [hunter] Anagram Generator

#!/usr/bin/perl
use warnings;
use strict;
START:
system('cls');
# ANAGRAM GENERATOR (A.G.)
print "Enter the phrase you want to turn into an anagram. \n";
print "-------------------------------------------------\n";
#INPUT THE PHRASE
my $phrase = <>;
print "\n";
#remove new line on end of input
chomp $phrase;
#CHANGE PHRASE INTO AN ARRAY
my @phrasearray = split(/ /, $phrase);
#create a loop that removes the first letter and capitalizes it and separates each word with a dot.
print 'The anagram for the phrase "', "$phrase", '" is:', "\n";
print "---------------------------------";
my $plength = length($phrase);
my $lines;
for($lines = 0; $lines < $plength; $lines++){
print "-";
}
print "\n";
foreach(@phrasearray){
print uc(substr($_, 0, 1)),".";
}
print "\n";
PLAYAGAIN:
print "\nMake another anagram? (y or n)\n";
my $replay = <>;
chomp $replay;
$replay = lc(substr($replay, 0, 1));
if ($replay ne "y" and $replay ne "n"){
print "Please select Y or N (yes or no).\n";
goto PLAYAGAIN;
}
if ($replay eq 'y'){
goto START;
} elsif ($replay ne 'y') {
print "\nOkay! Have a good day! \n";
exit;
}


mike.berding
New User

Apr 23, 2011, 7:06 PM


Views: 61415
Re: [mike.berding] Anagram Generator

I just realized that this was an acronym. not an anagram.


oops.

I failed.

this is an Acronym generator that works :P


FishMonger
Veteran

Apr 24, 2011, 9:30 PM


Views: 61367
Re: [mike.berding] Anagram Generator

Mike,

Did you realize that you were posting to a thread which is over 8 years old, and that the OP hasn't been back to this forum since asking for someone to provide the answer to this [his] homework assignment?

Looking over your code I see that you've use the strict and warnings pragmas, which is very good and often left out by others, but there are a few issues with your code.

This and most other programming forums provide "code tags" to be used to surround the code which retains the code indentation and offsets the code to make it easier to read. Please use those code tags. You should also add some vertical whitespace for the same reason.

The use of "goto" is almost never the proper choice for flow control; it create "spaghetti code". Proper use of subroutines and recursion is the more appropriate choice.

Your system call to clear the screen assumes that the user is running on Windows, which may or may not be the case. It would be better to use a more system independent approach.

Here's a rewrite of your script which could still use a couple improvements, but is cleaner and more up to today's standards.


Code
#!/usr/bin/perl 

use strict;
use warnings;


my $ClearScreen = $^O =~ /win32/i ? sub { system 'cls' }
: sub { system 'clear' };

CreateAcronym();

exit;

#########################

sub CreateAcronum {

$ClearScreen->();

my $prompt = "Enter the phrase you want to turn into an acronym.\n";
print $prompt, '-' x length($prompt);
chomp(my $phrase = <>);

my $acronym = join('', map { uc(substr($_,0,1)) . '.' } split " ", $phrase);

my $prompt2 = "The acronym for the phrase '$phrase' is:\n";
print "\n", $prompt2, '-' x length($prompt2), "\n$acronym\n\n";

CreateAcronym() if WantToPlayAgain();
}

sub WantToPlayAgain {
my $reply;
do {
print "Make another acronym? (y or n)\n";
chomp($reply = <>);

if ( $reply =~ /^y(es)?/i ) {
return 1;
}
if ( $reply =~ /^no?/i ) {
print "\nOkay! Have a good day! \n";
exit;
}
print "Please select Y or N (yes or no).\n";

} until $reply =~ /^y|n/i;
}



(This post was edited by FishMonger on Apr 24, 2011, 9:32 PM)


BillKSmith
Veteran

Aug 26, 2011, 6:02 AM


Views: 53101
Re: [mike.berding] Anagram Generator

A few years ago, I wrote a perl program for myself to solve the 'JUMBLE' puzzles in the newspaper. This is really the same thing as forming a one-word anagram. I have attached the code (with pod documentation.)

Anagram phrases may be a task for another day.
Good Luck,
Bill
Attachments: jumble.pl (2.56 KB)