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

后端 未结 10 951
我在风中等你
我在风中等你 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:26

    Perl 6 will improve things dramatically, but (so far?) Perl 5 has pretty limited slicing capability: you have to explicitly specify the indexes you want, and it can't be open-ended.

    So you'd have to do:

    @ar = ( "zero", "one", "two", "three", "four", "five", "six" );
    print @ar[ grep $_ % 2 == 0, 0..$#ar ]
    

提交回复
热议问题