How can I filter an array without using a loop in Perl?

前端 未结 4 1531
一整个雨季
一整个雨季 2020-12-29 03:34

Here I am trying to filter only the elements that do not have a substring world and store the results back to the same array. What is the correct way to do this

4条回答
  •  攒了一身酷
    2020-12-29 04:04

    That would be grep():

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
    my @narr = ( );
    
    print "@arr\n";
    @narr = grep(!/world/, @arr);
    print "@narr\n";
    

提交回复
热议问题