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


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

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.

Replies are listed 'Best First'.
Re^4: Need help with Peal!
by ivanzhibin (Initiate) on Nov 24, 2012 at 22:12 UTC
    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.

        sigh... i spent all night again by trying different code , still cant get solved. i dnn't know how to compare the user'input with the randomly generated letter. can you help me please :( .
        #!/usr/bin/perl -w ################################### # Name: scriptname.pl # Purpose: This script does… # Author: Ivan Zhibin He # Date written: 22/11/2012 ################################### ########################subroutinges####### 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; } #################################################### print "Random string: |".$string1."|".$string2."|".$string3."|".$string4."|".$string5."|".$s +tring6."|".$string7."|".$string8."|\n"; print "Are these letters Acceptale to make words?\n"; #################ask uer for input########## $newWord = <>; chomp $newWord; print "Please wait while i checking....\n"; @splitNewWord = split (//,$newWord); print @splitNewWord\n; #######Define string in hash 1-8#### %hashCheckLetter = ($string1,$string2,$string3,$string4,$string5,$stri +ng6,$string7,$string8); ########################code need to be fix!############## if (exists $hashCheckLetter{$splitNewWord}) { print "This is equal to %hashCheckLetter{$splitNewWord}\n"; } else { print "Not in hash\n"; }
Re^4: Need help with Peal!
by Anonymous Monk on Dec 01, 2012 at 14:04 UTC
    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.