
FishMonger
Veteran
Apr 24, 2011, 9:30 PM
Post #5 of 6
(62143 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.
#!/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)
|