How do I use grep or map to delete elements from an array or reference to array? I’m having problems using splice to remove one or more elements from a reference to array and would like to see whether grep or map can offer me a better solution.
@removedElements = grep /test/, @array;
You are saying
"array or reference array"like it was two different things. Which is confusing.I assume that since you have named your array
@removedElements, what you are trying to ask is how to remove the elements from@arrayand put them in@removedElements.A simple negation of the test will yield either list. You can also do a loop:
Which has the benefit of fewer checks being performed.
In order to use
splice, you would need to keep track of indexes, and I’m not sure its worth it in this case. It certainly would not make your code easier to read. Similarly, I doubtmapwould be much more useful than a regular loop.