dictionary using perl 
March 25th, 2008, 11:10 PM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | dictionary using perl
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 -
#!/usr/bin/perl
-
-
$data_file="bionicle.dat";
-
-
$input=<STDIN>;
-
-
open(DAT, $data_file) || die("Could not open file!");
-
@raw_data=<DAT>;
-
close(DAT);
-
-
foreach $word (@raw_data)
-
{
-
chop($word);
-
($name,$def)=split(/\|/,$word);
-
if ($name=$input) {
-
print "$name: $def";
-
sleep(10);
-
} else {
-
print "sorry, word not found";
-
sleep(3);
-
}
-
}
-
and dictionary.dat -
word1|definition1
-
word2|definition2
-
word3|definition3
-
so i that my script was nothing like what i wanted, but what did i want?
thanks in advance,
ian
| 
March 26th, 2008, 12:11 AM
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,031
| |
beginner mistake:
'=' 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':
look up perls string operators when you get a chance.
| 
March 26th, 2008, 12:30 AM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | Quote: |
Originally Posted by KevinADC beginner mistake:
'=' 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':
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?
| 
March 26th, 2008, 12:47 AM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| |
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.
| 
March 26th, 2008, 12:56 AM
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 898
| | | 
March 26th, 2008, 01:05 AM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| |
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.
| 
March 26th, 2008, 04:58 PM
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,031
| | 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.
| 
March 27th, 2008, 12:00 AM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | 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: -
-
kopaka|ice
-
pohatu|stone
-
tahu|burn
-
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.
| 
March 27th, 2008, 02:57 AM
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 898
| |
After you get your input into the $input variable use chomp. - my $file_name = 'f:/test_file.txt';
-
-
print 'Please enter a word?';
-
-
my $input = <STDIN>;
-
chomp($input);
-
-
open (my $FILE, '<', $file_name) || die "Can't open file $file_name: $!\n";
-
while (<$FILE>) {
-
chomp;
-
my ($name, $def) = split(/\|/);
-
-
if ($name eq $input) {
-
print "$name means $def\n";
-
exit;
-
-
..rest of code here...
-
-
}
-
close($FILE);
--Kevin
| 
March 27th, 2008, 04:39 AM
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | Quote: |
Originally Posted by eWish After you get your input into the $input variable use chomp. - my $file_name = 'f:/test_file.txt';
-
-
print 'Please enter a word?';
-
-
my $input = <STDIN>;
-
chomp($input);
-
-
open (my $FILE, '<', $file_name) || die "Can't open file $file_name: $!\n";
-
while (<$FILE>) {
-
chomp;
-
my ($name, $def) = split(/\|/);
-
-
if ($name eq $input) {
-
print "$name means $def\n";
-
exit;
-
-
..rest of code here...
-
-
}
-
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
| 
March 28th, 2008, 01:59 AM
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 898
| |
It is the same as witting the following. - open FILEHANDLE,MODE,EXPR
In theory it is a more secure way to open a file. That is according to ' Perl Best Practices'.
--Kevin
|  | | Thread Tools | Search this Thread | | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|