Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Need help with Peal!

by roboticus (Canon)
on Nov 24, 2012 at 13:19 UTC ( #1005371=note: print w/ replies, xml ) Need Help??


in reply to Need help with Peal!

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.


Comment on Re: Need help with Peal!
Re^2: Need help with Peal!
by ivanzhibin (Initiate) on Nov 24, 2012 at 16:42 UTC
    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.

        my $count1 = $string1 =~ tr/"a-z" || "A-Z"//; this code i was trying to compare with user input to prevent not using the same letter to make word(but doesn't work :( ) i tried 2 day alredy in many way almost cry out myself. thank you so much for pointing me to the right track i learned so much of keep trying the code ^_^. can you give me some tips of how to prevent not using the same generated letter to make word and give user error message if they did. i know i might need to use substring array? i will keep figthing tonite:)
        You know that you can do all this rand with a foreach loop, in no more than 6 lines. Look up using a subroutine to do a foreach (8 times) random character and store it in the return of the routine.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-07-28 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (207 votes), past polls