Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Need help with Peal!

by ivanzhibin (Initiate)
on Nov 24, 2012 at 22:12 UTC ( #1005432=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^4: Need help with Peal!
Re^5: Need help with Peal!
by roboticus (Canon) on Nov 25, 2012 at 02:07 UTC

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

        You really need to review how data in perl works--you're not going to figure it out by hacking on the keyboard.

        A couple procedural hints:

        • Insert print statements to print the values of variables so you can see what they contain. Sometimes they don't hold what you think they should. If you can't predict what values should be printed, then it's a sign that you don't understand something, and you need to learn it.
        • It'll be difficult to learn things if you're trying to learn them while writing a big program. If your attention is divided, you'll confuse yourself. Instead write tiny scripts to teach yourself individual concepts. Make them simple and easy to understand. Once you have a handle on the ideas, it's easier to use them in a larger piece of code.

        For example, here's a little program you could have written to investigate hashes. Once you understand how it works, it'll be easier to work on your project.

        $ cat t.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # some data to stick into the hash my $string1 = 'a'; # randomly generated my $string2 = 'f'; # randomly generated my $string3 = 'l'; # randomly generated my $string4 = 'g'; # randomly generated # stick them into the hash my %hashCheckLetter; $hashCheckLetter{$string1}=0; $hashCheckLetter{$string4}=0; $hashCheckLetter{$string3}=0; $hashCheckLetter{$string2}=0; # What does the hash actually have in it? print Dumper(\%hashCheckLetter); # what letters are in the hash? for my $letter ('a' .. 'h') { if (exists $hashCheckLetter{$letter}) { print "Found $letter!\n"; } else { print "Letter $letter not available\n"; } }

        When run, it produces this:

        $ perl t.pl $VAR1 = { 'l' => 0, 'a' => 0, 'g' => 0, 'f' => 0 }; Found a! Letter b not available Letter c not available Letter d not available Letter e not available Found f! Found g! Letter h not available

        ...roboticus

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

        print @splitNewWord\n;
        Syntax error. Did you even run it?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-08-30 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls