Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: Need help with Peal!

by roboticus (Canon)
on Nov 25, 2012 at 02:07 UTC ( #1005454=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^5: Need help with Peal!
Re^6: Need help with Peal!
by Anonymous Monk on Nov 25, 2012 at 22:54 UTC
    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.

        does it meant i have to assign this for 26 times from a-z ?? i know i am so stupid on this :( thanks for your time to helping me. can you show me a little more how to do this then i can do the rest. thank you very much. i cant move to next step until i solve this . 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)
        hi rob, here is my code , but it doesn;t show the repeated letter in those 8 randomly letter. can you help me how to implement that.
        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; ################################### # Name: scriptname.pl # Purpose: This script does… ################################### # some data to stick into the hash 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"; ##next # stick them into the hash my %hashCheckLetter; $hashCheckLetter{$string1}=0; $hashCheckLetter{$string2}=0; $hashCheckLetter{$string3}=0; $hashCheckLetter{$string4}=0; $hashCheckLetter{$string5}=0; $hashCheckLetter{$string6}=0; $hashCheckLetter{$string7}=0; $hashCheckLetter{$string8}=0; # What does the hash actually have in it? print Dumper(\%hashCheckLetter); # what letters are in the hash? for my $letter ('A'..'Z','a'..'z') { if (exists $hashCheckLetter{$letter}) { print "Found $letter!\n"; } else { print "Letter $letter not available\n"; } }
      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://1005454]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-10-25 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls