faqts : Computers : Programming : Languages : Perl : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 21 people (81%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I remove an element from an array?

Dec 27th, 2001 23:48
Capt. Mooboy, Per M Knutsen,


If you want to remove the element by its positon in the array, use the 
splice function. Example:
@someArray = splice(@someArray, 1);  # will remove the 1st element
# Example of removing a particular element in the array:
@foo = (1,2,3,4,5);
$arrayposition = 0;
foreach $i (@foo) {
        $i == 2 && splice(@foo,$arrayposition,1);
        ++$arrayposition;
}