Language question -- check for invalid hash key?

Discussion in 'Perl Misc' started by Richard G., Aug 11, 2005.

  1. Richard G.

    Richard G. Guest

    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.
     
    Richard G., Aug 11, 2005
    #1
    1. Advertising

  2. Richard G. wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 11, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M P
    Replies:
    1
    Views:
    520
  2. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  3. Une bévue
    Replies:
    5
    Views:
    165
    Une bévue
    Aug 10, 2006
  4. Siratinee Sukachai
    Replies:
    1
    Views:
    147
    7stud --
    Apr 22, 2011
  5. Antonio Quinonez
    Replies:
    2
    Views:
    184
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page