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. | [reply] [Watch: Dir/Any] [d/l] [select] |
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";
}
}
| [reply] [Watch: Dir/Any] [d/l] |
for(0..0){
0;
}
is the same as
my $i;
for($i = 0; $i < 1; $i++){
0;
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
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)
| [reply] [Watch: Dir/Any] |
No, the hash is just to keep track of the letters in use. You can also use the hash to tell if you're already using a letter when generating a random letter--if you've already used that random letter, try another random letter. If you think about how *you* would do it with dice, paper and pencil, you can tell the computer to do the same thing.
For example: You want to generate 8 different letters. How would we do it by hand? Like this:
- Get a new sheet of paper
- Do I have 8 letters on the sheet? If so, we're done, goto step!
- Roll the dice and select a random letter.
- Is the letter on the sheet? If not, write it on the sheet.
- go back to step 2.
- Return the list of numbers.
So for a piece of paper, I'll use a hash (it's much easier to use than individually named variables). So let's convert our manual procedure to code:
my @chars = ('A' .. 'Z');
my @hand = create_random_letter_list();
print join(", ", @hand), "\n";
sub create_random_letter_list {
# 1. Get a new sheet of paper
my %list;
# 2. Do I have eight letters?
while (8 > keys %list) {
# 3. Roll the dice and select a random letter
my $letter = $chars[int rand @chars];
# 4. If the letter is not on the sheet...
if (! exist $list{$letter}) {
# ...write it on the sheet
$list{$letter}=0;
}
} # 5. go back to step 2
# 6. Return the list
return keys %list;
}
Basically, when I code, I break the problem down into smaller problems. Just keep doing that. Eventually, the problems will be simple and you can write the code for it. In your assignment, you've got several steps. You just need to break each of those sections into smaller and smaller parts. But in order to do so, that means that you have to figure out how you would do it by hand. Then you can automate it.
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [Watch: Dir/Any] [d/l] |
print @splitNewWord\n;
Syntax error. Did you even run it? | [reply] [Watch: Dir/Any] [d/l] |