0

Brain getting foggy on this one. I wanted to take my dice game from using rand() to using a list of random values from random.org. I was able to retrieve the values just fine, I'm just hung up on the syntax to pop from the list.

Here's my function that's giving me fits:

sub roll_d
{
  return (pop($$dice_stack{@_[0]}));
  # Original code:
  #return (int(rand @_[0]) + 1);
}

Where $dice_stack is a pointer to a hash where the key is the dice type ('6' for d6, '20' for d20), and the value is an array of integers between 1 and the dice type.

4

2 に答える 2

7

$$dice_stack{@_[0]}--aka-$dice_stack->{@_[0]}はhashrefのVALUEです。そのため、必然的にスカラーになり、配列からポップ可能にすることはできません。

値が配列参照の場合は、参照を解除する必要があります。

  return ( pop(@{ $dice_stack->{ @_[0] } }) );

arrayrefでない場合は、他の方法でアクセスする必要があります。

また、これはゴルフっぽく見え始めています-ラインのノイズのこの時点で、より読みやすいコード(IMHO)に切り替えることをお勧めします:

  my ($dice_type) = @_;
  my $dice_list = $dice_stack->{$dice_type};
  return pop(@$dice_list);
于 2011-08-29T19:05:15.970 に答える
1

最初に配列を逆参照してみてください。

pop(@{$dice_stack{@_[0]}})
于 2011-08-29T19:05:06.520 に答える