cod*_*ero 0 arrays random perl hash hashref
大脑在这一个上变得模糊.我想把我的骰子游戏从使用rand()到使用来自random.org的随机值列表.我能够很好地检索这些值,我只是挂起了从列表中弹出的语法.
这是我的功能,让我适合:
sub roll_d
{
return (pop($$dice_stack{@_[0]}));
# Original code:
#return (int(rand @_[0]) + 1);
}
Run Code Online (Sandbox Code Playgroud)
其中$ dice_stack是指向散列的指针,其中键是骰子类型(d6为'6',d20为'20'),值为1和骰子类型之间的整数数组.
$$dice_stack{@_[0]}- aka $dice_stack->{@_[0]}- 是hashref中的值.因此,它必然是一个标量,不能是一个可弹出的数组.
如果值是数组引用,则需要取消引用:
return ( pop(@{ $dice_stack->{ @_[0] } }) );
Run Code Online (Sandbox Code Playgroud)
如果它不是arrayref,则需要以其他方式访问它.
此外,这开始看起来很高兴 - 在线噪声的这一点,我建议切换到更易读的代码(恕我直言):
my ($dice_type) = @_;
my $dice_list = $dice_stack->{$dice_type};
return pop(@$dice_list);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
610 次 |
| 最近记录: |