http://www.perlmonks.org?node_id=1005397


in reply to Re: Need help with Peal!
in thread Need help with Peal!

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"; }

Replies are listed 'Best First'.
Re^3: Need help with Peal!
by roboticus (Chancellor) on Nov 24, 2012 at 17:14 UTC

    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:)

        I'd suggest breaking the string apart into individual letters (see split) and then using a hash table to track the letters. You can also use a hash table to track the words used, as well. See perlfaq4, sections "How can I remove duplicate elements from a list or array?" through "How can I test whether two arrays or hashes are equal?" for the basic method and examples.

        I don't recall whose signature line it is, but it's particularly appropriate: Figure out how to do the task by hand, and *then* write the program to do that. If you don't understand a technique but try to apply it, then my signature line becomes more appropriate. ;^P

        ...roboticus

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

      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.

        Yes, I'm aware of that. The OP is having enough difficulties, however, that I didn't want to throw too many concepts at him at the same time.

        ...roboticus

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

Re^3: Need help with Peal!
by Anonymous Monk on Nov 24, 2012 at 16:52 UTC
    Protip, don't try and paste your assignment questions here, you should try and work them out yourself.