I need to walk over an array, and conditionally delete elements, in Perl. I know about slice, but am not sure how to use it in a foreach context.
In Ruby, there is reject!:
foo = [2, 3, 6, 7]
foo.reject! { |x| x > 3 }
p foo # outputs "[2, 3]"
Is there a Perl equivalent?
You can use Perl’s
grepwhich acts like an inverted reject: it will keep all items which satisfy the condition. Soreject x > 3must becomegrep x <= 3:The condition does not have to be a numeric comparison but can be anything that works in boolean context, e.g. matching against a regular expression.