有什么方法可以使用“常量”作为哈希键在Perl中? [英] Is there any way to use a "constant" as hash key in Perl?

查看:109
本文介绍了有什么方法可以使用“常量”作为哈希键在Perl中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如:

<$ p $有什么方法可以使用常量作为散列键? p> 使用常数X => 1;

my%x =(X =>'X');

以上代码会创建一个以X作为键的散列,而不是1作为键。然而,我想使用常量X的值作为键。

解决方案

使用常量实际上会生成常量的子程序。



要做你想做的事情,你需要明确地调用sub:

 使用常量X => 1; 

my%x =(& X =>'X');

 使用常数X => 1; 

my%x =(X()=>'X');


Is there any way to use a constant as a hash key?

For example:

use constant X => 1;

my %x = (X => 'X');

The above code will create a hash with "X" as key and not 1 as key. Whereas, I want to use the value of constant X as key.

解决方案

use constant actually makes constant subroutines.

To do what you want, you need to explicitly call the sub:

use constant X => 1;

my %x = ( &X => 'X');

or

use constant X => 1;

my %x = ( X() => 'X');

这篇关于有什么方法可以使用“常量”作为哈希键在Perl中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆