Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to NOT quote constants in hash keys

Reply
Thread Tools

how to NOT quote constants in hash keys

 
 
Martin Adler
Guest
Posts: n/a
 
      06-28-2004
What can I "use" to make the following program work as I want it to?

#!/usr/bin/perl -w
use strict;
use constant N => 17 ;
my %x ;
$x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
print keys %x ;

The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
But how can I tell it to interpret constants as their (constant)
value?
I want Perl to interpret $x{N} as $x{17}.

One of the goals of perl is "to make easy tasks easy and difficult
tasks possible". It seems to me that it is POSSIBLE to use constants
as hash keys (e.g.: $x{ (N) }), but it does not look EASY.

Or what else should I do?
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      06-28-2004
On Mon, 28 Jun 2004 09:59:53 -0700, Martin Adler wrote:
> #!/usr/bin/perl -w
> use strict;
> use constant N => 17 ;
> my %x ;
> $x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
> print keys %x ;
>
> The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
> But how can I tell it to interpret constants as their (constant)
> value?
> I want Perl to interpret $x{N} as $x{17}.


Why didn't you read the documentation before posting? It would have saved
you a lot of time;

perldoc constant


--
Tore Aursand <>
"Measuring programming progress by lines of code is like measuring
aircraft building progress by weight." (Bill Gates)
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-28-2004
On Mon, 28 Jun 2004, Martin Adler wrote:

> What can I "use" to make the following program work as I want it to?
>
> #!/usr/bin/perl -w
> use strict;
> use constant N => 17 ;
> my %x ;
> $x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
> print keys %x ;
>
> The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
> But how can I tell it to interpret constants as their (constant)
> value?
> I want Perl to interpret $x{N} as $x{17}.
>
> One of the goals of perl is "to make easy tasks easy and difficult
> tasks possible". It seems to me that it is POSSIBLE to use constants
> as hash keys (e.g.: $x{ (N) }), but it does not look EASY.
>
> Or what else should I do?


Perl is trying to make something easy. It's just not what you want to be
easy in this case. Perl's auto-quoting is taking over here, which makes
it easy for the vast majority of cases, when we want $x{N} to mean
$x{'N'}, instead of the remarkably few cases when we want $x{N} to mean
$x{(WhateverValueConstantNRepresents)}.

You have three basic options:
write $x{N()} instead of $x{N}
write $x{+N} instead of $x{N}
use the ReadOnly module from CPAN instead of the constant pragma. This
defines constants that look like Perl variables instead of C Macros:

use ReadOnly $N => 17;
$x{$N} = 25;


Paul Lalli
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      06-28-2004
On Mon, 28 Jun 2004, Paul Lalli wrote:
> use the ReadOnly module from CPAN instead of the constant pragma. This
> defines constants that look like Perl variables instead of C Macros:
>
> use ReadOnly $N => 17;
> $x{$N} = 25;


I should clarify two things:
1) the above is not actually correct syntax.
Read the Readonly documentation for the correct syntax.

2) Readonly.pm comes with a significant performance hit over constant.pm.
Read teh Readonly documentation for details.

Paul Lalli
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
replacing strange quote with standard quote Stimp ASP .Net 2 09-20-2006 10:01 AM
Single Quote Versus Double Quote In A href link knee-dragger@hotmail.com HTML 3 06-13-2006 12:42 AM
Problem using defined constants as hash keys bkimelman@hotmail.com Perl Misc 2 04-15-2005 01:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57