Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-16-2005, 09:59 AM   #1
LQ Newbie
Registered: Jun 2005
Posts: 7

Rep: Reputation: 0
Deleting elements from array in perl with splice

I'm having trouble with deleting elements from an array from within a foreach loop.

For example:

@abc = qw(a b c d e f g h);
print"There are $#abc elements in \@abc\n";
$abc_counter = "0";
foreach $letter (@abc){
   print"\@abc[$abc_counter] has $letter\n";
   if($letter eq "e"){
      print"E must be removed.\n";
   } else {

print"There are $#abc elements in \@abc\n";
foreach $alpha (@abc) {
Prints out:
There are 7 elements in @abc
@abc[0] has a
@abc[1] has b
@abc[2] has c
@abc[3] has d
@abc[4] has e
E must be removed.
@abc[4] has g
@abc[5] has h
There are 6 elements in @abc
The element holding "f" never gets checked. I guess this is because when I splice the array to remove the element it changes the indexes of the array without foreach knowing that any changes have been made. Is there some magic $? value that perl uses isn the foreach loop for an index? Or should I be going about this some other way. I don't want to delete the value in the array, I want to remove the element completely.

Any ideas?

Old 08-16-2005, 07:36 PM   #2
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
I notice this statement in the perldocs:
If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.
Perhaps you can selectively copy elements into a new array instead.
Old 08-16-2005, 10:57 PM   #3
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Rep: Reputation: 15
The reason the last element hasn't been counted for you is because $#array_name does NOT return the length of an array. Rather, it returns the last index of an array. This means that for an 8-element array, it will return 7, since the first index is always 0. To get the length of an array in a scalar index, you can simply use @array_name. You can also force scalar output with the scalar function, ie scalar( @array_name ).

Now, as spooon pointed out in his link, the variable you were looking for in your foreach loop was $_, which is the default variable perl uses in many of its built-in functions. However, as he also mentioned, it is against convention to manipulate array elements in foreach loops. If you wish to actually change array values, use a for-loop. Foreach loops should be reserved for occasions when your array elements remain unchanged.

Here is a version of your code that should work. I actually opted for a while-loop over a for-loop after testing though, as a for-loop would require $i to be decremented in order to work properly. Otherwise, the element directly after e would be skipped. However, it's not good form to manipulate the control variable in a for-loop. Thus, I felt a while variant was in order.

use strict;
use warnings;

my @abc = ('a' .. 'h');

print "There are ", scalar(@abc), " elements in \@abc\n";

my $pointer = 0;
while ($pointer <= $#abc) {
   print "\@abc[$pointer] is $abc[$pointer]\n";

   if ($abc[$pointer] eq 'e') {
      print "E must be removed.\n";
      splice(@abc, $pointer, 1);
   else {

print "There are ", scalar(@abc), " elements in \@abc\n";
foreach (@abc) {
   print "$_\n";
And the output:
There are 8 elements in @abc
@abc[0] is a
@abc[1] is b
@abc[2] is c
@abc[3] is d
@abc[4] is e
E must be removed.
@abc[4] is f
@abc[5] is g
@abc[6] is h
There are 7 elements in @abc
Hope that helps...

Last edited by jayemef; 08-16-2005 at 11:13 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
odd behaviour of array elements in c++ markhod Programming 4 03-14-2005 09:58 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
perl split(@array)?? wtf? bobjones Programming 5 09-23-2002 09:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration