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
10 2508
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.
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?
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.
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.
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.
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.
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chris |
last post by:
One nice thing about Perl that is helpful when tallying things up by
type is that if you increment a hash key and the key does not exist,
Perl puts a one there. So you can have code that does...
|
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: rickle |
last post by:
I'm trying to compare sun patch levels on a server to those of what sun
is recommending. For those that aren't familiar with sun patch
numbering here is a quick run down.
A patch number shows...
|
by: David Bear |
last post by:
I know there must be a better way to phrase this so google understands, but
I don't know how.. So I'll ask people.
Assume I have a list object called 'alist'.
Is there an easy way to create a...
|
by: Rodd Snook |
last post by:
I have an application which makes extensive use of the
Scripting.Dictionary object. I'm not doing anything silly like putting
them outside the page scope -- just creating quite a few of them and...
|
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a...
|
by: Tom Grove |
last post by:
I have a server program that I am writing an interface to and it returns
data in a perl dictionary. Is there a nice way to convert this to
something useful in Python?
Here is some sample data:...
|
by: Chris Rebert |
last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote:
Right, this clobbers the existing entry with this new blank one. This
is evidenced by the fact that you're performing an...
|
by: John Townsend |
last post by:
Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions.
I have two text files. Each file contains data like this:
Test file 1234 4567...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |