Connecting Tech Pros Worldwide Help | Site Map

dictionary using perl

  #1  
Old March 26th, 2008, 12:10 AM
Newbie
 
Join Date: Nov 2007
Posts: 9
i am trying to write a dictionary using perl, the program would use a primary .pl file, and a text file, designated .dat

i have no programing background, and am trying to teach myself perl, and c++, so excuse my ignorance.

i have experimented with some scripts, i have figured out why my last one didn't work, but i have no idea what to do. this was my last attempt:

dictionary.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $data_file="bionicle.dat";
  4.  
  5. $input=<STDIN>;
  6.  
  7. open(DAT, $data_file) || die("Could not open file!");
  8. @raw_data=<DAT>;
  9. close(DAT);
  10.  
  11. foreach $word (@raw_data)
  12. {
  13.  chop($word);
  14.  ($name,$def)=split(/\|/,$word);
  15.  if ($name=$input) {
  16.   print "$name: $def";
  17.   sleep(10);
  18.   } else {
  19.   print "sorry, word not found";
  20.   sleep(3);
  21.   }
  22. }
  23.  
and dictionary.dat

Expand|Select|Wrap|Line Numbers
  1. word1|definition1
  2. word2|definition2
  3. word3|definition3
  4.  
so i that my script was nothing like what i wanted, but what did i want?

thanks in advance,

ian
  #2  
Old March 26th, 2008, 01:11 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: dictionary using perl


beginner mistake:

Expand|Select|Wrap|Line Numbers
  1. if ($name=$input) {
'=' is the assignment operator so the above is always true because you are assigning the value of $input to $name. Probably you want to use 'eq':

Expand|Select|Wrap|Line Numbers
  1. if ($name eq $input) {
look up perls string operators when you get a chance.
  #3  
Old March 26th, 2008, 01:30 AM
Newbie
 
Join Date: Nov 2007
Posts: 9

re: dictionary using perl


Quote:
Originally Posted by KevinADC
beginner mistake:

Expand|Select|Wrap|Line Numbers
  1. if ($name=$input) {
'=' is the assignment operator so the above is always true because you are assigning the value of $input to $name. Probably you want to use 'eq':

Expand|Select|Wrap|Line Numbers
  1. if ($name eq $input) {
look up perls string operators when you get a chance.
oops actually i think i meant to use "==", i knew that. isn't "eq" used for numeric equallity?
  #4  
Old March 26th, 2008, 01:47 AM
Newbie
 
Join Date: Nov 2007
Posts: 9

re: dictionary using perl


by the way, my intended result was for the script to print the word originally typed, and the definition supplied by the dictionary.dat file. actually there were quite a few problems with that script.

basically my question is how can i make perl read from another file, such as the one i supplied, search for a word found before the pipe divider, and print the text following the pipe.
  #5  
Old March 26th, 2008, 01:56 AM
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 901

re: dictionary using perl


perlop Equality Operators.

--Kevin
  #6  
Old March 26th, 2008, 02:05 AM
Newbie
 
Join Date: Nov 2007
Posts: 9

re: dictionary using perl


ok thanks, i guess i shouldn't question people more experienced than me lol. i got that completely backward.

however upon using the "eq" operator, my script just ran indefinitely, without ever printing an indication of true or false. i believe if it was working correctly, although this is not what i wanted, wouldn't it print "word not found", for each line in the dictionary.dat file? but when i run it, it never prints anything.
  #7  
Old March 26th, 2008, 05:58 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: dictionary using perl


Quote:
Originally Posted by islayer
ok thanks, i guess i shouldn't question people more experienced than me lol. i got that completely backward.

however upon using the "eq" operator, my script just ran indefinitely, without ever printing an indication of true or false. i believe if it was working correctly, although this is not what i wanted, wouldn't it print "word not found", for each line in the dictionary.dat file? but when i run it, it never prints anything.
post a few (2 or 3) sample lines from the dictionary file and show what input you give your script.
  #8  
Old March 27th, 2008, 01:00 AM
Newbie
 
Join Date: Nov 2007
Posts: 9

re: dictionary using perl


Quote:
Originally Posted by KevinADC
post a few (2 or 3) sample lines from the dictionary file and show what input you give your script.
the dictionary is a dictionary of maori words.

dictionary.dat:

Expand|Select|Wrap|Line Numbers
  1.  
  2. kopaka|ice
  3. pohatu|stone
  4. tahu|burn
  5.  
this is just an example there are a lot of words before, after, and in between.

input into dictionary.pl:

"tahu" without quotes

it then runs infinitely without printing, and obviously not ending.

(edit) actually after some time it does end. so i guess it is going through my entire dictionary.dat file.
  #9  
Old March 27th, 2008, 03:57 AM
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 901

re: dictionary using perl


After you get your input into the $input variable use chomp.

Expand|Select|Wrap|Line Numbers
  1. my $file_name = 'f:/test_file.txt';
  2.  
  3. print 'Please enter a word?';
  4.  
  5. my $input = <STDIN>;
  6. chomp($input);
  7.  
  8. open (my $FILE, '<', $file_name) || die "Can't open file $file_name: $!\n";
  9. while (<$FILE>) {
  10.     chomp;
  11.     my ($name, $def) = split(/\|/);
  12.  
  13.     if ($name eq $input) {
  14.         print "$name means $def\n";
  15.         exit;
  16.  
  17.     ..rest of code here...
  18.  
  19. }
  20. close($FILE);
--Kevin
  #10  
Old March 27th, 2008, 05:39 AM
Newbie
 
Join Date: Nov 2007
Posts: 9

re: dictionary using perl


Quote:
Originally Posted by eWish
After you get your input into the $input variable use chomp.

Expand|Select|Wrap|Line Numbers
  1. my $file_name = 'f:/test_file.txt';
  2.  
  3. print 'Please enter a word?';
  4.  
  5. my $input = <STDIN>;
  6. chomp($input);
  7.  
  8. open (my $FILE, '<', $file_name) || die "Can't open file $file_name: $!\n";
  9. while (<$FILE>) {
  10.     chomp;
  11.     my ($name, $def) = split(/\|/);
  12.  
  13.     if ($name eq $input) {
  14.         print "$name means $def\n";
  15.         exit;
  16.  
  17.     ..rest of code here...
  18.  
  19. }
  20. close($FILE);
--Kevin
ok, thank you very much! most of the code make sense to me, but like i said, im a n00b. the parts i don't understand are '<' and < and > around $FILE

thanks a lot,

ian
  #11  
Old March 28th, 2008, 02:59 AM
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 901

re: dictionary using perl


It is the same as witting the following.
Expand|Select|Wrap|Line Numbers
  1. open FILEHANDLE,MODE,EXPR
In theory it is a more secure way to open a file. That is according to 'Perl Best Practices'.

--Kevin
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dictionary .keys() and .values() should return a set [with Python 3000 in mind] vatamane@gmail.com answers 14 July 4th, 2006 08:35 AM
python vs perl lines of code Edward Elliott answers 82 May 24th, 2006 07:55 AM
dictionary comparison rickle answers 7 July 19th, 2005 01:58 AM
data management with python from perl ben moretti answers 2 July 18th, 2005 04:27 AM