[Chicago-talk] Delete element from array

David Young davidy at nationalcycle.com
Fri Jul 11 11:03:33 PDT 2008


delete is meant for hashes, not arrays.

Why not push the items you want to keep onto a new array, and then overwrite the original?  Like this:

foreach $symbol (@symbol) {
            if (foo1) {
            next;
            };
            if (foo2) {
            next;
            };
            .
            .
            .
            push(@keep, $symbol);
}
@symbol = @keep;


>>> mdemir3000 at gmail.com 07/11/08 12:51PM >>>
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;
            };
            .
            .
            .
}

Many thanks for your time.

Mike



More information about the Chicago-talk mailing list