There is more than one way to do it, so here's another one, inspired by this answer: Use grep to keep the elements you want. Since Perl only supports deleting elements from the array you're iterating over in certain situations, this does not require you to know which situations those are :) .
use strict; use warnings;
my @array1 = ("cat 2", "dog 3#move", "tiger 4#move", "lion 10");
my @array2;
@array1 = grep { # We are going to search over @array1 and only keep some elements.
if (/(.*)#move/) { # If this is one we want to move...
push @array2, $1; # ... save it in array2...
0; # ... and do not keep it in array1.
} else {
1; # Otherwise, do keep it in array1.
}
} @array1;
# Debug output - not required
print "Array 1\n";
print join "\n", @array1;
print "\nArray 2\n";
print join "\n", @array2;
print "\n";
Output:
Array 1
cat 2
lion 10
Array 2
dog 3
tiger 4
$2is expected to be.@array1contains? It is not the correct way to instantiate a Perl array, and the regex you provide would not match since there's no comma before#move. (and a word character is\w, not/w)