LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-16-2005, 09:59 AM   #1
signalno9
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:

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.
 
Old 08-16-2005, 07:36 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
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.
 
Old 08-16-2005, 10:57 PM   #3
jayemef
Member
 
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.
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...

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


Reply


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


All times are GMT -5. The time now is 03:44 PM.

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