![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 10
Rep Power: 0
![]() |
Dictionary Program
I don't know what is wrong with this program. It is supposed to read in a word and definition like so
word - definition and store in a hash. I have no clue why it doesn't work. It will open the hash but will not save anything. ###############################################
# Author: Blackwolf189@gmail.com
# Start Date: April 13, 2005
# End Date:
# Description: A program designed to extend
# your vocabulary.
###############################################
system('cls');
$wordlist = $ENV{"Input"}."WordList.dbm";
dbmopen(%dictionary,$wordlist,0666) || die("Failed To Create Output File");
while($option = printHead())
{
if($option == 2)
{
print "Type each word and definition on a separte line\n";
print "Type a hyphen between each word and definition\n";
print "Type :END: to finish\n\n";
while($entry = <STDIN>)
{
chomp($entry);
if($entry eq ":END:"){last}
@entry = split(/-/,$entry);
$dictionary{$entry[0]} = $entry[1] && print "Word Added\n\n"
|| warn "Error $!. Word not Added\n\n";
}
}
elsif ($option == 3)
{
print "Type the word you would like to look up:\n";
lookUp($entry = <STDIN>);
}
elsif ($option == 4)
{
print "Type the definition after each word\n";
wordtest();
}
}
sub printHead
{
print <<HEADING;
Vocab 1 2 3
1. Import Word List
2. Add Words
3. Look Up Word
4. Test on words
5. Random Word
6. Exit
HEADING
print "Type the number of your selection: ";
$selection = <STDIN>;
if($selection != 6){return $selection;}
exit();
}
sub lookUp
{
chomp($_[0]);
if ($dictionary{$_[0]} eq undef)
{print "Dictionary does not contain $_[0]\n";}
else
{print "$_[0] = $dictionary{$_[0]}\n";}
}
sub wordtest
{
while($word,$definiton = each(%dictionary))
{
print "$word: ";
<STDIN>;
print $definition;
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
instead of using
$dictionary{$_[0]} eq undef!defined($dictionary{$_[0]}) Looks like you have some other bugs.. the way you have this written $dictionary{$entry[0]} = $entry[1] && print "Word Added\n\n"
|| warn "Error $!. Word not Added\n\n";why not add an option to print the entire dictionary (for debugging if nothing else) elsif ($option == 7)
{
foreach my $word (sort keys %dictionary) {
print "($word) - ($dictionary{$word})\n";
}
}Last edited by spydoor; May 9th, 2005 at 8:46 AM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 10
Rep Power: 0
![]() |
I am unsure of how my code assigns the result of an expression to dictionary. Please explain. i thought that @entry = split(/-/,$entry) would store the word in $entry[0] and the definition in $entry[1]
Last edited by Blackwolf189; May 9th, 2005 at 8:53 AM. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
The split is working exactly how you think it is.
entry[0] is the word and entry[1] is the definition the problem is the assignment. an example. $dictionary{test} = 'test's definition' && print "Good" || print "Bad"the entire right hand side of the assignemnt is being evaluated as an expression. so in this case it is true (1). it's like saying $var = "A" && "B" || "C"
#same as
$var = ("A" && "B" || "C")
#same as
$var = (true && true || true) #where true is the boolean result of the statementTo make var = "A"; you'd wrap the assignment in paranthesis ($var = "A") && "B" || "C" if you want to keep similar syntax just wrap the assignment in paranthesis, although I think the test for good or bad is unnecessary and probably not working as you expect. (you need yet another set of parens around the assignment and print "Good" statment for the logic to be correct) ($dictionary{$entry[0]} = $entry[1]) && print "Word Added\n\n"
|| warn "Error $!. Word not Added\n\n";I personally find this difficult to read. It's not a bad thing to have more lines of code especially if it makes the logic easier to follow. (($x="test") && print "Assignment Worked") || print "Assignment Failed";
# same logic, different sytnax..
if($x="test") {
print "Assignment Worked";
}else{
print "Assignment Failed";
}Last edited by spydoor; May 9th, 2005 at 11:28 AM. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2005
Posts: 10
Rep Power: 0
![]() |
Thanks for the indepth explanation. It all makes sense now.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|