Language question -- check for invalid hash key?

R

Richard G.

Hi.

Kinda new to Perl. I've read the Programming Perl book (2nd ed, not
newest.) I have a script I recently wrote that stores key/data pairs in a
hash for use as a look-up table during script execution. The script reads a
large data file, extracts string fragments from each line, chops the last
character from a string and uses that character as the key to lookup
information in my hash. Basicaly, it looks like this:

my %Hash_Table = (
A => "Data for A",
B => "Data for B",
C => "Data for C"
);

$String _Fragment = substr ($Long_String, $x, $y);
$Key_Char = chop $String_Fragment;
$Data_Info = $Hash_Table{$Key_Char};

Trouble is I can't guarentee the character I chop from String_Fragment will
be a valid Key in the hash. I don't mind if I run across an invalid key,
but how can I gracefully first check if it's a valid key before I attempt to
access the data in the hash? The perl book I have doesn't explain the
result if you use in invalid Key in a hash variable.

The reason I ask is because I capture all STDOUT and STDERR to files, and I
run perl with the -w flag so I can capture all warning messages. Well, with
the -w flag, perl complains I'm assigning an unitialized value to a
variable.

The solution I figured was to wrap the hash access in a conditional if
statement:

$Data_Info = $Hash_Table{$Key_Char} if ($Hash_Table{$Key_Char});

This works, in that it wont kick a warning message if I use an invalid key.
I was just curious if there's a better way, and to do the verification and
assignment all in one statment without needing to access the keypair twice?
What I did feels sorta ameturish. If there's something more elegent (or
more complex for job security) I'm curious what it is.
 
J

John W. Krahn

Richard said:
Kinda new to Perl. I've read the Programming Perl book (2nd ed, not
newest.) I have a script I recently wrote that stores key/data pairs in a
hash for use as a look-up table during script execution. The script reads a
large data file, extracts string fragments from each line, chops the last
character from a string and uses that character as the key to lookup
information in my hash. Basicaly, it looks like this:

my %Hash_Table = (
A => "Data for A",
B => "Data for B",
C => "Data for C"
);

$String _Fragment = substr ($Long_String, $x, $y);
$Key_Char = chop $String_Fragment;

Why not use unpack() and get both values in one statement?
$Data_Info = $Hash_Table{$Key_Char};

Trouble is I can't guarentee the character I chop from String_Fragment will
be a valid Key in the hash. I don't mind if I run across an invalid key,
but how can I gracefully first check if it's a valid key before I attempt to
access the data in the hash?

perldoc -f exists
The perl book I have doesn't explain the
result if you use in invalid Key in a hash variable.

Look for the word "autovivification".



John
 

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
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top