Dave's Brain

Browse - Programming Tips - How do I delete an element from a perl array?

Date: 2010feb11
Created: 2009jul4
Language: perl

Q.  How do I delete an element from a perl array?

A.  To delete element $i from array @a do this:

splice(@a, $i, 1);
This will decrement the number of elements in your array by 1. There is also a "delete" command in perl but it does not do quite this. Note that splice() works in-place. So do NOT do this:
@a = splice(@a, $i, 1); # WRONG, WRONG, WRONG

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.