Skip to main content

Deleting Items from Perl Array

It's straightforward to delete items from Perl associative arrays (hashes), by using $ary_name->delete()

For normal arrays there is no such syntax, but the grep() function comes to rescue sometimes.

my $search_str = 'whatever';
my @new_ary = grep(!/$search_str/, @old_ary);

The above will return a @new_ary which is devoid of all instances of $search_str (effectively deleting them).

Here's the reference - http://perl.about.com/od/perltutorials/qt/perlgrep.htm

Comments