Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 8th, 2005, 6:37 PM   #1
Blackwolf189
Newbie
 
Join Date: May 2005
Posts: 10
Rep Power: 0 Blackwolf189 is on a distinguished road
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;
   }
}
Blackwolf189 is offline   Reply With Quote
Old May 9th, 2005, 8:32 AM   #2
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
instead of using
$dictionary{$_[0]} eq undef
use
!defined($dictionary{$_[0]})
unfortunately I can't give you an explanation why.... I don't know



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";
You are assigning the result of an expression (true or false) to $dictionary{$entry[0]}... adding a set of paranthesis should solve that problem



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.
spydoor is offline   Reply With Quote
Old May 9th, 2005, 8:51 AM   #3
Blackwolf189
Newbie
 
Join Date: May 2005
Posts: 10
Rep Power: 0 Blackwolf189 is on a distinguished road
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.
Blackwolf189 is offline   Reply With Quote
Old May 9th, 2005, 10:38 AM   #4
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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 statement
literals in Perl are evaluted to true....so $var = 1; after that line.
To 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.
spydoor is offline   Reply With Quote
Old May 9th, 2005, 7:25 PM   #5
Blackwolf189
Newbie
 
Join Date: May 2005
Posts: 10
Rep Power: 0 Blackwolf189 is on a distinguished road
Thanks for the indepth explanation. It all makes sense now.
Blackwolf189 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC