Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing an element from an array 2

Status
Not open for further replies.
May 3, 2003
90
CA
Hello all,

I have an array called @list. I want to remove certain elements from this array like so.

foreach (@list) {

if (/windss\/comp/i) {
pop @list # troubles with this here
}

}

Im not sure if pop is the way to go. I dont want to just undef the element. I just want to remove the element if the condition is true. Help is needed.

thanks in advnace
 
foreach $line (@list) {
 if ($line =~ m/windss\/comp/ig) {
  $line = "";
 }
}
 
join array to a string using join command. Use regular expression to remove element from string. Then split string into an array again.

Following example actually puts the 'RemoveMe' string to top of array. Just remove the line $joinArray = "$1".$joinArray to remove the element from array.
Ex.
@testArray = ('one','two','RemoveMe','three','four');
printf("\@testArray:mad:testArray\n");
$joinArray = join "/", @testArray;
printf("\$joinArray:%s\n",$joinArray);
$joinArray =~ s/(RemoveMe\/)//;
$joinArray = "$1".$joinArray;
(@finalArray) = split ('/', $joinArray);
printf("After removing and adding WebHome \@finalArray:mad:finalArray\n");
printf("going through finalArray in for loop:\n");
foreach $loop (@finalArray) {
print $loop."\n";
}
 
@list = ('abcde','abcwindssdef','xyz','12345');

$list_length = @list;

print "There are $list_length items in the array...\n\n";

for ($x=0; $x < $list_length; $x++) {

print &quot;item $x: $list[$x]\n&quot;;

if ($list[$x] =~ m/windss/i) { # you'll need to update this

delete $list[$x];

print &quot;\$list[$x} $list[$x] $element DELETED\n&quot;;

}

}
 
For arcnon and duncdude, I would appreciate if you guys could read my post before answering it. I specifically said I dont want those elements to be undef and the examples you gave me, the elements are undef and not removed from the array. They still hold a null value.

I will try out ur example smithcw.

thanks.
 
Not to be picky, but smithcw's example isn't very scalable.

The makers of perl must have seen you having this problem in their crystal ball, becuase they made, for you, two functions: grep() and splice()

Use grep:
Code:
@foo = grep(!/windss\/comp/, @bar);  
# weed out those elements.
OR you could use splice():
Code:
for($x = 0; $x <= $#list; $x++){
    splice(@list, $x, 1) if $list[$x] =~ m|windss/comp|;
}
Hope this helps.

--jim
 
Both of Coderifous's examples are great...

@list = ('abcde','windss/comp','xyz','comp','qwerty');

print &quot;original list: &quot;; print (join &quot;|&quot;, @list); print &quot;\n&quot;;

@newlist = grep(!/windss\/comp/, @list);

print &quot; updated list: &quot;; print (join &quot;|&quot;, @newlist); print &quot;\n&quot;;

Duncan
 
Ahhh! Thanks so much Coderifous.
Your examples work great. Just one more question. Correct me if Im wrong. But I think I saw someone use the pop function to remove any element of an array and not just the last one. Is that possible or was I too drunk that night ??

Thanks again guys for your patience !
 
POP - This function pops and returns the last value of the array, shortening the array by one element. It is not possible to use this function to remove any element.

I hope i'm not mistaken in stating this...

And as for Coderifous - you're welcome. You sure know your stuff!

Duncan
 
Too drunk. :) pop() always removes the last element from an array, and shift() always removes the first one, shift()ing all remaining elements 'down' one.

If one had an afinity for pop(), and didn't want to hurt it's feelings, I suppose it could be done only half moronically:

for(@fizzles_shizzles_and_gizzles_list){
push @no_shizzles_list, $_;
pop @no_shizzles_list if /shizzle/;
}

This is silly though. One would HAVE to be inebriated to use this method. Or really like push-pops.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top