[Chicago-talk] Delete element from array

Leland Johnson leland at protoplasmic.org
Fri Jul 11 11:39:56 PDT 2008


This is a perfect place to use the grep function. You want an array of good
symbols, so grep can select all elements that are not bad.
my @sym = ...;

sub symbol_is_bad {
    my $symbol = shift;

    if (foo1) {
        return 1;
    }
    if (foo2) {
        return 1;
    }
    ...

    return;
}

my @good_sym = grep { not symbol_is_bad($_) } @sym;

--
Leland Johnson

On Fri, Jul 11, 2008 at 12:51 PM, Mike Demir <mdemir3000 at gmail.com> 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;
>             };
>             .
>             .
>             .
> }
>
> Many thanks for your time.
>
> Mike
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20080711/d27b70b0/attachment-0001.html>


More information about the Chicago-talk mailing list