Is there a compact Perl operation to slice alternate elements from an array?

后端 未结 10 916
我在风中等你
我在风中等你 2020-12-15 20:37

If I have an array myarray in Python, I can use the slice notation

myarray[0::2]

to select only the even-indexed elements. For

10条回答
  •  时光说笑
    2020-12-15 21:34

    If you don't care about the order, and if the odd-numbered elements of the list are unique, you can concisely convert the array to a hash and take the values:

    @even_elements = values %{{@array}};
    @odd_elements = keys %{{@array}};
    

    (No, this is not a serious answer)

提交回复
热议问题