Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

quotation mark as key

by ic23oluk (Sexton)
on Aug 06, 2017 at 09:50 UTC ( #1196840=perlquestion: print w/replies, xml ) Need Help??
ic23oluk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

is it possible to assing a hash with a quote sign as keys? i.e.  my $hash = (""" => 1) The reason for my question is I try to assing hash harboring the ascii signs as keys, and the corresponding numbers as values

Replies are listed 'Best First'.
Re: quotation mark as key
by Corion (Pope) on Aug 06, 2017 at 10:16 UTC

    First off, if you are using a hash reference, you need braces, not parentheses:

    my $hash = { foo => 'bar', };

    There are several approaches you can use.

    Use single quotes for a double quote:

    my $hash = { '"' => 1, };

    Quote the double quote:

    my $hash = { "\"" => 1, };

    Use chr:

    my $hash = { chr(34) => 1, };

    Use alternative quotes:/p>

    my $hash = { q(") => 1, };
Re: quotation mark as key
by roboticus (Chancellor) on Aug 06, 2017 at 10:19 UTC


    Sure, you can do that:

    my %t = ('"'=>1);

    You just have to either escape it correctly or use alternative quoting mechanisms.

    However, making a hash to hold a character and it's corresponding ASCII value isn't necessary since the ord function will give you the ASCII value for an ASCII character.


    When your only tool is a hammer, all problems look like your thumb.

Re: quotation mark as key
by huck (Vicar) on Aug 06, 2017 at 10:24 UTC

    Yes, but your hash assignment and scalar constant are wrong.

    use strict; use warnings; use Data::Dumper; my $hash = {"\"" => 1,':'=>2}; print Dumper($hash);
    Any characters can be used in keys, but the scalar constants must be quoted/escaped properly when using one to assign the key.

Re: quotation mark as key
by kcott (Chancellor) on Aug 08, 2017 at 06:23 UTC

    G'day ic23oluk,

    I see you've been given lots of advice on how to handle that specific character.

    From your question, it appears that you may be attempting to hard-code each character and ASCII value. That's a lot of effort, and quite unnecessary, for your stated task: "ascii signs as keys, and the corresponding numbers as values". You can do that with a single statement:

    my %hash = map { chr $_ => $_ } 32 .. 126;

    That will give you a table of all, printable, 7-bit ASCII characters and their corresponding decimal values. You can test it like this:

    $ perl -e 'use Data::Dump; my %h = map { chr $_ => $_ } 32 .. 126; dd +\%h' { " " => 32, "!" => 33, "\"" => 34, ... "|" => 124, "}" => 125, "~" => 126, }

    Take a look at sprintf, if you want to format the values as hexadecimal, Unicode code points, HTML entity references, or something else. For example,

    $ perl -e 'use Data::Dump; my %h = map { chr $_ => sprintf "U+%04X", $ +_ } 32 .. 126; dd \%h' { " " => "U+0020", "!" => "U+0021", "\"" => "U+0022", ... "|" => "U+007C", "}" => "U+007D", "~" => "U+007E", }

    [Aside: I'm guessing English isn't your first language. The word you were looking for in your OP is "assign" ('n' and 'g' order reversed). The word "assing" means something else: I'll leave you to follow that link if want a chuckle. :-)]

    — Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1196840]
Approved by 1nickt
Front-paged by 1nickt
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-07-19 19:44 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (417 votes). Check out past polls.