W
Wondering
I'm teaching myslf Perl using the Camel book. I'm trying to load some
data, and I think a hash is a good solution for mapping values. I did
one simple hash in the program, and it worked as expected. Now I have a
larger lookup table, and rather than write it into the program, I'd
prefer to store the lookup table in a text file, and load it at
runtime. I've tried a couple of different methods, and I thought I had
it working at one point. Then, a couple of days went by, and now I
can't get what I want at all. I separated the code for that hash into a
separate Perl script so I could test it, and nothing I've tried (book
in hand) has given the result I would like. I expect it's something
simple a newbie like me would miss.
Here's an abbreviated version of the table from the text file (filename
is "sufhash"):
'149' => ' x'
'150' => ' y'
'151' => ' z'
'152' => 'AA'
I've also written this file with commas at the end of each line, but
this is the current iteration.
Here's the Perl script I'm using to test:
#!/usr/contrib/bin/perl
%sufmap = (`cat /"full path"/sufhash`) ;
$hashcnt = (keys(%sufmap)) ;
# @keys = keys %sufmap ;
# @val = values %sufmap ;
# while (@keys) {
# print pop(@keys), '=', pop(@val), "\n" ;
# }
# foreach $key (sort keys %sufmap) {
# print $key. '='. $sufmap{$key}, "\n";
# $sleep = (`sleep 2`) ;
# }
while (($key,$value) = each %sufmap) {
print "$key = $value\n" ;
}
You can see that I've tried (and commented out) several ways of
displaying the contents of the hash. As near as I can tell, each method
of displaying works about as well as any other, so I suspect the
problem is in the way I've written the text file the hash is loaded
from. What I get is a key/value pair on a line, followed by a "=" sign,
and another key/value pair on the next line. What I expect is a key,
the "=" sign, and the corresponding value all on one line. I'm not so
particular about the output of the test script, if I can get the hash
to work in the program.
Thanks to all who take the time to read all this, and a bigger thanks
to those who respond. I'll be happy to provide additional info as
requested.
data, and I think a hash is a good solution for mapping values. I did
one simple hash in the program, and it worked as expected. Now I have a
larger lookup table, and rather than write it into the program, I'd
prefer to store the lookup table in a text file, and load it at
runtime. I've tried a couple of different methods, and I thought I had
it working at one point. Then, a couple of days went by, and now I
can't get what I want at all. I separated the code for that hash into a
separate Perl script so I could test it, and nothing I've tried (book
in hand) has given the result I would like. I expect it's something
simple a newbie like me would miss.
Here's an abbreviated version of the table from the text file (filename
is "sufhash"):
'149' => ' x'
'150' => ' y'
'151' => ' z'
'152' => 'AA'
I've also written this file with commas at the end of each line, but
this is the current iteration.
Here's the Perl script I'm using to test:
#!/usr/contrib/bin/perl
%sufmap = (`cat /"full path"/sufhash`) ;
$hashcnt = (keys(%sufmap)) ;
# @keys = keys %sufmap ;
# @val = values %sufmap ;
# while (@keys) {
# print pop(@keys), '=', pop(@val), "\n" ;
# }
# foreach $key (sort keys %sufmap) {
# print $key. '='. $sufmap{$key}, "\n";
# $sleep = (`sleep 2`) ;
# }
while (($key,$value) = each %sufmap) {
print "$key = $value\n" ;
}
You can see that I've tried (and commented out) several ways of
displaying the contents of the hash. As near as I can tell, each method
of displaying works about as well as any other, so I suspect the
problem is in the way I've written the text file the hash is loaded
from. What I get is a key/value pair on a line, followed by a "=" sign,
and another key/value pair on the next line. What I expect is a key,
the "=" sign, and the corresponding value all on one line. I'm not so
particular about the output of the test script, if I can get the hash
to work in the program.
Thanks to all who take the time to read all this, and a bigger thanks
to those who respond. I'll be happy to provide additional info as
requested.