Showing posts with label ARRAY. Show all posts
Showing posts with label ARRAY. Show all posts

Tuesday, October 23, 2012

PERL Remove array element

Deleting value from an array is easy if you know the element/index but if you only got the element/index value and not sure of it's location, you'll need a few more lines to do this. Here's a code snippet you can use if you need to delete an element to your array, given that you only know the element value.

---------------------
      my $index;
                foreach(0..$#array_list){
                        if ($array_list[$_] =~ /^$element_value/) {
                                $index = $_;
                        }
                }
                splice @array_list, $index, 1;

---------------------

This can used as a sub within your code and will require two arguments (@array_list --> the array where you need to delete something and $element_value --> the value you want to delete from you array)