Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use numbers in certain instances for perl hash keys?

Tags:

key

perl

Quick question...
Why does the first key work but not the rest? That is, the second key throws a syntax error. I've used numbers as keys before, but as soon as I write 'to' the script turns black (that is, not comment colour which is used for keys normally). If I take away the 'to' it works and throws an error on the next key.

Can I not have a number and letter combination starting with a number?

my %ranges = (
    under10 => "x < 10000",
    10to20  => "10000 <= x < 20000",
    20to30  => "20000 <= x < 30000",
    30to40  => "30000 <= x < 40000",
    40to50  => "40000 <= x < 50000",
    50to60  => "50000 <= x < 60000",
    60to70  => "60000 <= x < 70000",
    70to80  => "70000 <= x < 80000",
    80to90  => "80000 <= x < 90000",
    90to100 => "90000 <= x < 100000",
    100plus => "100000 <= x",
);
like image 257
dgBP Avatar asked Nov 20 '25 17:11

dgBP


2 Answers

Put them in quotes. The documentation says:

The => operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string if it's a bareword that would be a legal simple identifier.

Identifiers have to begin with a letter or underscore, so 10to30 is not a legal identifier. As a result, it doesn't get converted to a string.

like image 70
Barmar Avatar answered Nov 23 '25 07:11

Barmar


you need to qoute them :)

my %ranges = (
    'under10' => "x < 10000",
    '10to20'  => "10000 <= x < 20000",
    '20to30'  => "20000 <= x < 30000",
    '30to40'  => "30000 <= x < 40000",
    '40to50'  => "40000 <= x < 50000",
    '50to60'  => "50000 <= x < 60000",
    '60to70'  => "60000 <= x < 70000",
    '70to80'  => "70000 <= x < 80000",
    '80to90'  => "80000 <= x < 90000",
    '90to100' => "90000 <= x < 100000",
    '100plus' => "100000 <= x",
);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!