Perl hash question

J

Jeff Vacha

Hello All,

I am new to Perl and am really struggling to understand why the below
does not print an email address as I believe it should. When the
whole hash is displayed, the email address appears. However, when I
try to "look it up" in the hash, it doesn't appear. Any ideas what I
am doing wrong.

Thanks!

Jeff Vacha
(e-mail address removed)



# This is SybPerl code. If you don't no Sybperl, I think you can
still understand the question without understanding this part...

$dbh->ct_execute("select parm_name, parm_value from behavior_parms
where key=$key");
while($dbh->ct_results($restype) == SUCCEED) {
next unless $dbh->ct_fetchable($restype);
while (%dat = $dbh->ct_fetch()) {
$parms{$dat[0]} = $dat[1];
}
}




while(($TheKey, $TheVal) = each(%parms))
{print "$TheKey is the only key for $TheVal\n"}

$email_address = $parms{'email address'};
print "\nEmail: $email_address\n";


<<OUTPUT EXAMPLE:

email address is the only key for (e-mail address removed)
zip result is the only key for y
input table is the only key for map_cd
output file name is the only key for file_to_email.txt

Email:


Note.. the while loops works but the print of just the email address
doesn't.
 
G

Glenn Jackman

Jeff Vacha said:
When the whole hash is displayed, the email address appears.
However, when I try to "look it up" in the hash, it doesn't appear. [...]
while(($TheKey, $TheVal) = each(%parms))
{print "$TheKey is the only key for $TheVal\n"}

$email_address = $parms{'email address'};
print "\nEmail: $email_address\n";


<<OUTPUT EXAMPLE:

email address is the only key for (e-mail address removed)
zip result is the only key for y
input table is the only key for map_cd
output file name is the only key for file_to_email.txt

Email:

Note your output: "email address is the..."
Do you see that there are 2 spaces after address?
Your hash does not contain the key 'email address', but it does contain
the key 'email address '.

You should get into the habit of
use strict;
use warnings;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top