Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Need help with Peal!

by ivanzhibin (Initiate)
on Nov 24, 2012 at 12:56 UTC ( #1005370=perlquestion: print w/ replies, xml ) Need Help??
ivanzhibin has asked for the wisdom of the Perl Monks concerning the following question:

here is my set task: i am stuck on the step 3 which check generated letter can only use once. how can i implement that? 1. Output 8 randomly generated characters and ask user if the letters are acceptable 2. Input word 3. Check word can be made from the 8 randomly generated characters (make sure letters only used once in the randomly generated word are only used once in the entered word) 4. If Step 3. is OK, check word is in dictionary. If the word is not in the dictionary then ask the user if it is a real word - if it is then it is added to a dictionary file. 5. If Step 4. is OK, add number of points (one point per letter in the word entered) to a score and ensure that the same word cannot be entered again 6. Repeat steps 2.-5. until the user cannot think of any more words and enters a sentinel value 7. The final score for that game should be displayed and added to a high scores table (in the correct position - highest at the top, lowest at the bottom) 8. Display the high scores table (suitably formatted) 9. Ask the user if they would like another game, if they do repeat steps 1. to 8.

Comment on Need help with Peal!
Re: Need help with Peal!
by roboticus (Canon) on Nov 24, 2012 at 13:19 UTC

    Ivanzhibin:

    I'd use a hash to keep track of the letters you generate, and the exists function. If you check the perl FAQs perlfaq1, perlfaq2, etc., you may find some other bits of information to simplify your task.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      than i will keep trying on. i am a beginger of perl ^_^ this is the code i wrote..but not working.
      #!/usr/bin/perl -w ################################### # Name: scriptname.pl # Purpose: This script does… # Author: Ivan Zhibin He # Date written: 22/11/2012 ################################### my $string1 = rand_string1(); sub rand_string1 { my @chars = ('A'..'Z'); my $length = 0; my $string = ''; for (0..$length) { $string1 .= $chars[int rand @chars]; } return $string1; } ################################### my $string2 = rand_string2(); sub rand_string2 { my @chars = ('A'..'Z'); my $length = 0; my $string2 = ''; for (0..$length) { $string2 .= $chars[int rand @chars]; } return $string2; } ################################### my $string3 = rand_string3(); sub rand_string3 { my @chars = ('A'..'Z'); my $length = 0; my $string3 = ''; for (0..$length) { $string3 .= $chars[int rand @chars]; } return $string3; } ################################### my $string4 = rand_string4(); sub rand_string4 { my @chars = ('A'..'Z'); my $length = 0; my $string4 = ''; for (0..$length) { $string4 .= $chars[int rand @chars]; } return $string4; } ################################### my $string5 = rand_string5(); sub rand_string5 { my @chars = ('A'..'Z'); my $length = 0; my $string5 = ''; for (0..$length) { $string5 .= $chars[int rand @chars]; } return $string5; } ################################### my $string6 = rand_string6(); sub rand_string6 { my @chars = ('A'..'Z'); my $length = 0; my $string6 = ''; for (0..$length) { $string6 .= $chars[int rand @chars]; } return $string6; } ################################### my $string7 = rand_string7(); sub rand_string7 { my @chars = ('A'..'Z'); my $length = 0; my $string7 = ''; for (0..$length) { $string7 .= $chars[int rand @chars]; } return $string7; } ################################### my $string8 = rand_string8(); sub rand_string8 { my @chars = ('A'..'Z'); my $length = 0; my $string8 = ''; for (0..$length) { $string8 .= $chars[int rand @chars]; } return $string8; } ################################### ############to compare count###### my $count1 = $string1 =~ tr/"a-z" || "A-Z"//; my $count2 = $string2 =~ tr/"a-z" || "A-Z"//; my $count3 = $string3 =~ tr/"a-z" || "A-Z"//; my $count4 = $string4 =~ tr/"a-z" || "A-Z"//; my $count5 = $string5 =~ tr/"a-z" || "A-Z"//; my $count6 = $string6 =~ tr/"a-z" || "A-Z"//; my $count7 = $string7 =~ tr/"a-z" || "A-Z"//; my $count8 = $string8 =~ tr/"a-z" || "A-Z"//; print "The 1st string has $count1 letter\n"; print "The 2nd string has $count2 letter\n"; print "The 3rd string has $count3 letter\n"; print "The 4th string has $count4 letter\n"; print "The 5th string has $count5 letter\n"; print "The 6th string has $count6 letter\n"; print "The 7th string has $count7 letter\n"; print "The 8th string has $count8 letter\n"; print "Random string: |".$string1."|".$string2."|".$string3."|".$string4."|".$string5."|".$s +tring6."|".$string7."|".$string8."|\n"; print "Are these letters Acceptale to make words?\n"; $newWord = <>; chomp $newWord; ########################code need tobe fix!############## if ($newWord =~ /$string1[$string2] || [$string3] || [$string4] || [$s +tring5] || [$string6] || [$string7] || [$string8]] || $string2[$string1] || [$string3] || [$string4] || [$string5] || [$s +tring6] || [$string7] || [$string8]] || $string3[$string1] || [$string2] || [$string4] || [$string5] || [$s +tring6] || [$string7] || [$string8]] || $string4[$string1] || [$string2] || [$string3] || [$string5] || [$s +tring6] || [$string7] || [$string8]] || $string5[$string1] || [$string2] || [$string3] || [$string4] || [$s +tring6] || [$string7] || [$string8]] || $string6[$string1] || [$string2] || [$string3] || [$string4] || [$s +tring5] || [$string7] || [$string8]] || $string7[$string1] || [$string2] || [$string3] || [$string4] || [$s +tring5] || [$string6] || [$string8]] || $string8[$string1] || [$string2] || [$string3] || [$string4] || [$s +tring5] || [$string6] || [$string7]]/i) { print "Good\n"; } else { print "Bad\n"; }
        Protip, don't try and paste your assignment questions here, you should try and work them out yourself.

        Ivanzhibin:

        Please go back and edit your node to add code tags (<c>your code goes here</c>) to your post: It's pretty difficult to make sense of it.

        I went ahead and reformatted some of it on my machine to see what you're doing, but didn't finish. I saw a couple of things that you'll want to learn before proceeding.

        First: the point of a subroutine is to let you reuse code, rather than writing it over and over. Specifically, you don't need all the rand_string1 through rand_string8 subroutines. They all do the exact same thing. This does the same thing as the first section of your program:

        my $string1 = rand_string(); my $string2 = rand_string(); my $string3 = rand_string(); my $string4 = rand_string(); my $string5 = rand_string(); my $string6 = rand_string(); my $string7 = rand_string(); my $string8 = rand_string(); sub rand_string { my @chars = ('A'..'Z'); my $length = 0; my $temp_string = ''; for (0..$length) { $temp_string .= $chars[int rand @chars]; } return $temp_string; }

        The variable name you use inside of your subroutine doesn't have to be related to the variable name outside of your subroutine.

        Next, I don't know what you think this line does:

        my $count1 = $string1 =~ tr/"a-z" || "A-Z"//;

        But I'm pretty sure it doesn't do what you want. You'll want to read over the documentation for tr in the documentation (perldoc -f perlop).

        I'll check back later and see how things are going.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Need help with Peal!
by ww (Bishop) on Nov 24, 2012 at 15:10 UTC
    Smells like homework.

    You'll learn more by actually trying to use whatever your tutor and text have already covered rather than asking us to do it for you.

    OTH, if this assignment is really for a Peal! class, perhaps "BellRingers.com" would be a better source. :(

Re: Need help with Peal!
by chickenlips (Initiate) on Nov 24, 2012 at 16:32 UTC
    Perhaps you should try working out the solution for yourself and not submitting the whole Assignment. P.
Re: Need help with Peal!
by LanX (Canon) on Nov 24, 2012 at 18:13 UTC
    Better tell your teachers: "Peal is dead!" ;-)

    Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005370]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-07-25 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (174 votes), past polls