[Chicago-talk] Delete element from array

Steven Lembark lembark at wrkhors.com
Fri Jul 11 15:37:03 PDT 2008


Mike Demir wrote:
> Hi:
> 
> So here is my quandry. I have a list of stock symbols stored in array 
> (@sym), and am looking to test each symbol against a series of 
> conditions  (foo1, foo2....). When the condition is met, I would like to 
> remove that symbol and return to the top of the loop to start working on 
> the next symbol. If the symbol fails all the test conditions, it should 
> remain in the array. I'm having no trouble getting the test conditions 
> to work, or the looping to work. However I can't seem to remove the 
> symbol from the array. It's a conceptual thing at this point, so a 
> solution is nice but not required. Need to understand.
> 
> What I'm seeing is that only the $symbol[0] gets deleted.
> 
> So here's what I had attempted...
> 
> foreach $symbol (@symbol) {
>             if (foo1) {
>             delete $symbol[];
>             next;
>             };
>             if (foo2) {
>             delete $symbol[];
>             next;
>             };
>             .
>             .
>             .
> }


Iterate the index and use splice; moderately
expensive but it should work.

       for( 0 .. $#symz )
       {
             if( whatever )
             {
                   my $data    = splice @symz, $_, 1;

                   ...
             }
       }

That or if the symbols are unique, use them
as hash keys (see preveious message).


More information about the Chicago-talk mailing list