CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



hunter
New User

Jan 23, 2003, 5:07 PM

Post #1 of 6 (48938 views)
Anagram Generator Can't Post

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

Post #2 of 6 (48927 views)
Re: [hunter] Anagram Generator [In reply to] Can't Post

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

Post #3 of 6 (42728 views)
Re: [hunter] Anagram Generator [In reply to] Can't Post

#!/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

Post #4 of 6 (42727 views)
Re: [mike.berding] Anagram Generator [In reply to] Can't Post

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

Post #5 of 6 (42679 views)
Re: [mike.berding] Anagram Generator [In reply to] Can't Post

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

Post #6 of 6 (34413 views)
Re: [mike.berding] Anagram Generator [In reply to] Can't Post

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)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives