Filtering elements of an array with elements of another array in Perl 6

后端 未结 3 1196
渐次进展
渐次进展 2021-01-18 09:09

I want to filter elements of @array which begin with elements of @search:

my @array = \         


        
3条回答
  •  失恋的感觉
    2021-01-18 09:48

    For this kind of search, you can easily use index

    say (@array X @search).grep( { defined($^a[0].index($^a[1])) } )
        .grep( { $^a[0].index($^a[1]) == 0 } );
    

    This creates a list of Cs, and seeks the second element of the pair within the first; it returns only the list of those that appear in the first position. defined is needed because it will return Nil if it does not find it. It's not faster than your alternative above, but it's in the same ballpark, with 0.03 system time and ~ 0.30 seconds

提交回复
热议问题