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

Good day,

Simple question, so bear with me. When using certain characters as the key in a hash (such as ' or !), why is it not recognized? ie.

$something{'that\'s it'}
$something{'thats it\!'}
(playing with the quotes and such).

Again something simple but I can't seem to find something to describe this properly. Any help is appreciated.

Replies are listed 'Best First'.
Re: Hash key/value
by almut (Canon) on Nov 26, 2009 at 17:04 UTC

    What exactly do you mean by "not recognized"? Seems to work fine:

    #!/usr/bin/perl $something{'that\'s it'} = "foo"; $something{'thats it\!'} = "bar"; use Data::Dumper; print Dumper \%something; __END__ $VAR1 = { 'that\'s it' => 'foo', 'thats it\\!' => 'bar' };
      My apologies, was in a rush when I wrote that.

      I have a file foo.txt that contains a phrase such as "that's it!". I read the contents of the file, and use the filename (foo) as the value in a hash, with the key being the contents:
      $key = "that's it!"; #contents from file foo.txt $value = "foo"; #filename $something{$key} = $value; print $something{$key};
      When I try to print the above, nothing is there, however when I remove the ' and !, "foo" is printed. I will play with the suggestions in a short while however. Might realize it's just how I'm doing it. Thank you for the quick replies.

        Maybe you should write some code that fails as you describe? Adding strictures (use strict; use warnings;) to your code and running it I get:

        use strict; use warnings; my %something; my $key = "that's it!"; #contents from file foo.txt my $value = "foo"; #filename $something{$key} = $value; print $something{$key};

        which prints:

        foo

        just as I would expect. Can you show me equivelent code that fails?


        True laziness is hard work
        FireBird34: Your subsequent post indicates your problem is resolved, but just for the record:
        >perl -wMstrict -le "my $key = \"that's it!\"; my $value = \"foo\"; my %something; $something{$key} = $value; print $something{$key}; use Data::Dumper; print Dumper \%something; " foo $VAR1 = { 'that\'s it!' => 'foo' };
Re: Hash key/value
by AnomalousMonk (Archbishop) on Nov 26, 2009 at 17:46 UTC
    FireBird34: As mentioned above, you don't say how  ' and  ! fail to behave as you 'expect'! Can you give some examples?

    Remember, the strings in your OP are single-quoted strings, and the  \ (backslash) has very limited function when used as an escape in a single-quoted string. In essence,  \ only escapes itself and  ' in such a string.

    See the discussion of  '' and  q{} in the Quote and Quote like Operators section (and in Quote Like Operators) of perlop.

      For example,

      print 'that\'s it', "\n"; print 'that\'s it\!', "\n"; print 'that\'s it\\!', "\n";
      that's it \ escapes the delimiter (') that's it\! \ wasn't followed by the delimiter (') or \ that's it\! \ escapes \

      Contrast with double-quotes:

      print "\"that's it!\"\n"; print "\"that's it\!\"\n"; print "\"that's it\\!\"\n";
      "that's it!" \ escapes " "that's it!" \ escapes ! "that's it\!" \ escapes \
      Problem was resolved. Problem turned out to be me. I didn't properly take into account the extra '. Stupid mistake :) Thank you for the help.