Aquí intento filtrar solo los elementos que no tienen una subcadena world
y almacenar los resultados en la misma matriz. ¿Cuál es la forma correcta de hacer esto en Perl?¿Cómo puedo filtrar una matriz sin usar un bucle en 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.
$
Quiero pasar la matriz como argumento a una subrutina.
Yo sé una manera sería algo como esto
$ 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
$
quiero saber si hay una manera de hacerlo sin el bucle (usando algún tipo de filtrado simple).
Es bueno tener los enlaces de referencia aquí. – Wolf
Esa sintaxis de coincidencia negativa '!/Match /' fue crítica. Había estado considerando mapas y otras soluciones, pero todos parecían demasiado torpes para Perl. – davenpcj