Como posso filtrar uma matriz sem usar um loop no Perl?
Aqui estou tentando filtrar apenas os elementos que não possuem uma substringworld
e armazene os resultados novamente na mesma matriz. Qual é a maneira correta de fazer isso no Perl?
$ cat test.pl
use strict;
use warnings;
my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
print "@arr\n";
@arr =~ v/world/;
print "@arr\n";
$ perl test.pl
Applying pattern match (m//) to @array will act on scalar(@array) at
test.pl line 7.
Applying pattern match (m//) to @array will act on scalar(@array) at
test.pl line 7.
syntax error at test.pl line 7, near "/;"
Execution of test.pl aborted due to compilation errors.
$
Eu quero passar a matriz como argumento para uma sub-rotina.
Eu sei que uma maneira seria algo assim
$ cat test.pl
use strict;
use warnings;
my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
my @arrf;
print "@arr\n";
foreach(@arr) {
unless ($_ =~ /world/i) {
push (@arrf, $_);
}
}
print "@arrf\n";
$ perl test.pl
hello 1 hello 2 hello 3 world1 hello 4 world2
hello 1 hello 2 hello 3 hello 4
$
Quero saber se existe uma maneira de fazer isso sem o loop (usando alguma filtragem simples).