Is there a less clumsy alternative for copying up to “n” array elements?

前端 未结 5 1150
名媛妹妹
名媛妹妹 2021-01-11 13:57

In a world of many @choices
With a $limit to what one can do,
Life proposes many @options
But at time

5条回答
  •  忘掉有多难
    2021-01-11 14:11

    I'd probably use splice:

    my @choices = splice ( @options, 0, $limit ); 
    

    Note that splice acts like shift/pop and modifies the source array - if that's undesirable, then copy it first.

提交回复
热议问题