Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem using defined constants as hash keys

Reply
Thread Tools

Problem using defined constants as hash keys

 
 
bkimelman@hotmail.com
Guest
Posts: n/a
 
      04-15-2005
I am using perl version 5.8.6 on a Windows XP PC.

I am having a problem using defined constants as hash keys.

Here is an examaple to demonstrate the problem

====================================

use strict;
use constant;
use Class::Struct;
use Data:umper;

use constant OPT_BOOLEAN => 'b';
use constant OPT_STRING => 's';
use constant OPT_INTEGER => 'i';
use constant OPT_NOPARM => '-';

my ( %types );

%types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,
OPT_INTEGER => "integer" , OPT_NOPARM => "" ) ;

print Dumper(\%types),"\n";

exit 0;

==============================

When I run the above perl script the output from the print statement
shows the "names" of the defined constants as the values of the hash
keys. Can you get it to use the value of the defined constants as the
values of the hash keys ?

Currently I am forced into doing this until I c an solve this problem :

@parm_types = ( OPT_BOOLEAN , OPT_STRING , OPT_INTEGER , OPT_NOPARM );
@parm_type_notes = ( "boolean flag" , "string" , "integer" , "" );
%types = map { $parm_types[$_] , $parm_type_notes[$_] } ( 0 ..
$#parm_types);

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am having a problem using defined constants as hash keys.
>
> Here is an examaple to demonstrate the problem
>
> ====================================
>
> use strict;
> use constant;
> use Class::Struct;
> use Data:umper;
>
> use constant OPT_BOOLEAN => 'b';
> use constant OPT_STRING => 's';
> use constant OPT_INTEGER => 'i';
> use constant OPT_NOPARM => '-';
>
> my ( %types );
>
> %types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,
> OPT_INTEGER => "integer" , OPT_NOPARM => "" ) ;
>
> print Dumper(\%types),"\n";
>
> exit 0;
>
> ==============================
>
> When I run the above perl script the output from the print statement
> shows the "names" of the defined constants as the values of the hash
> keys. Can you get it to use the value of the defined constants as the
> values of the hash keys ?


Yes, you can do

%types = ( OPT_BOOLEAN() => "boolean flag", ...
--------------------------^^

See the "Bugs" section in "perldoc constant".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-15-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

> I am having a problem using defined constants as hash keys.


> use constant OPT_BOOLEAN => 'b';
> use constant OPT_STRING => 's';


> %types = ( OPT_BOOLEAN => "boolean flag" , OPT_STRING => "string" ,



> When I run the above perl script the output from the print statement
> shows the "names" of the defined constants as the values of the hash
> keys.



Because => is auto-quoting the constant's names.


> Can you get it to use the value of the defined constants as the
> values of the hash keys ?



Sure, you can either write it so that auto-quoting won't quote:

%types = ( OPT_BOOLEAN() => "boolean flag", OPT_STRING() => "string",

or don't use the operator that does auto-quoting:

%types = ( OPT_BOOLEAN, "boolean flag", OPT_STRING, "string",


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
constants defined in Kernel are also defined in Object? Paul Brannan Ruby 13 03-04-2008 03:09 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM
how to NOT quote constants in hash keys Martin Adler Perl Misc 3 06-28-2004 05:41 PM



Advertisments