Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Hash keys not DWIMming

by sflitman (Hermit)
on Oct 07, 2010 at 05:21 UTC ( #863907=note: print w/ replies, xml ) Need Help??


in reply to Hash keys not DWIMming

I agree with Dave that line 6 has a problem, the solution being to put the 512_x64 in single quotes like you did for the hash definition. This code works fine:

#!/usr/bin/perl use warnings; use strict; my %h = ( '512_x64' => '24', 'world' => 'hello'); print $h{world}, "\n"; # line 5 print $h{'512_x64'}, "\n"; # line 6
So you can use a key that starts with a digit!
HTH,
SSF


Comment on Re: Hash keys not DWIMming
Download Code
Re^2: Hash keys not DWIMming
by syphilis (Canon) on Oct 07, 2010 at 07:04 UTC
    the solution being to put the 512_x64 in single quotes

    Yes, I know that works. It's just that I thought steps had been taken to avoid the need for quoting hash keys .... that's obviously not so and we're left with what looks like a dog's breakfast when it comes to deciding if a key needs to be quoted or not.

    After spending a number of minutes looking at this, the best rule of practice I can come up with is "if the name of the key matches /^[0-9_\.]/ then quote it" - otherwise there's too many "ifs and buts" involved in trying to determine whether it will work without quotes.

    Cheers,
    Rob
      It's just that I thought steps had been taken to avoid the need for quoting hash keys

      From "Learning Perl (5th)", p 95:

      ...the keys are always converted to strings. So, if you used the numeric expression 50/20 as the key, it would be turned into the three-character string "2.5"...

      And on p. 254, in a section titled "Unquoted Hash Keys":

      Perl offers many shortcuts that can help programmers. Here's a handy one: you may omit the quote marks on some hash keys.

      Of course, you can't omit the quote marks on just any key, since a hash key may be any arbitrary string. But keys are often simple. If the hash key is made up of nothing but letters, digits, and underscores without starting with a digit, you may be able to omit the quote marks. This kind of simple string without quote marks is called a bareword, since it stands alone without quotes.

      ...But beware: if there's anything inside the curly braces besides a bareword, Perl will interpret it as an expression.

      It's just that I thought steps had been taken to avoid the need for quoting hash keys .... that's obviously not so and we're left with what looks like a dog's breakfast when it comes to deciding if a key needs to be quoted or not.

      The rule seems pretty straightforward to me. If the hash key is a valid identifier, it doesn't need quoted. Otherwise it does. Has always worked fine for my usage and always does what I want.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863907]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-07-06 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls