pawel84 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'd like to store special characters key: %(Math_value) value: pi $hashTable{%(Math_value)} = "pi"; How can I do it without compilers exceptions?
  • Comment on Hash Table - can special characters be stored as Keys?

Replies are listed 'Best First'.
Re: Hash Table - can special characters be stored as Keys?
by Corion (Patriarch) on Jun 02, 2009 at 08:40 UTC

    Perl hash keys are strings, and in most cases, you need to quote them whenever Perl does not recognize them as single words:

    $hashTable{'%(Math_value)'} = "pi";

    As a quick rule, Perl will accept all ASCII words as an unquoted hash key that matche /^\w+$/.

Re: Hash Table - can special characters be stored as Keys?
by vinoth.ree (Monsignor) on Jun 02, 2009 at 09:00 UTC

    When specifying string hash keys, we should use single '' quotes. This is the best choice.If we leave the quotes sometimes, we have to remember to add them when our key contains internal spaces, special characters.We should use quotes in those cases. Its also highlights the syntax.

    Vinoth,G
      Thanks for the answers! That works for me.