LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Deleting elements from array in perl with splice (http://www.linuxquestions.org/questions/programming-9/deleting-elements-from-array-in-perl-with-splice-353827/)

signalno9 08-16-2005 09:59 AM

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:

Code:

@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";
      splice(@abc,$abc_counter,1);
  } else {
      $abc_counter++;
  }
}

print"There are $#abc elements in \@abc\n";
foreach $alpha (@abc) {
  print"$alpha\n";
}

Prints out:
Code:

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
a
b
c
d
f
g
h

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?

Thanks.

spooon 08-16-2005 07:36 PM

I notice this statement in the perldocs:
Quote:

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.

jayemef 08-16-2005 10:57 PM

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.
Code:

#!/usr/bin/perl

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 {
      $pointer++;
  }
}

print "There are ", scalar(@abc), " elements in \@abc\n";
foreach (@abc) {
  print "$_\n";
}

And the output:
Code:

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
a
b
c
d
f
g
h

Hope that helps...


All times are GMT -5. The time now is 06:41 AM.