kathys39 has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of company names with various companies in them, in random fashion. Each day I get a company name that i want to search on, then delete from this array. I don't know the index number, so using splice is throwing me for a loop. i've tried this:
@allcompanies = grep { $_ != $co } @allcompanies
but this puts the company i want to delete into my array not the opposite. Do I need to cycle thru each of my companies, find the array #, then use splice? Seems like there should be a shorter way...

Replies are listed 'Best First'.
Re: splice help - deleting specific element
by ikegami (Patriarch) on Oct 17, 2008 at 16:49 UTC

    Switch to a string comparison instead of a numerical comparison and it'll work. Didn't you notice all the "Argument isn't numeric in numeric ne (!=)" warnings?

    my @allcompanies = qw( Acme Google Yahoo ); my $to_remove = 'Acme'; my @selectcompanies = grep { $_ ne $to_remove } @allcompanies; print(join(', ', @selectcompanies), "\n"); # Google, Yahoo
Re: splice help - deleting specific element
by toolic (Bishop) on Oct 17, 2008 at 16:50 UTC
    It seems to work for me if I use warnings; use strict;, then change != to ne:
    use strict; use warnings; use Data::Dumper; my $co = 'b'; my @allcompanies = qw(a b c d); print Dumper(\@allcompanies); @allcompanies = grep { $_ ne $co } @allcompanies; print Dumper(\@allcompanies); __END__ $VAR1 = [ 'a', 'b', 'c', 'd' ]; $VAR1 = [ 'a', 'c', 'd' ];
Re: splice help - deleting specific element
by mpeever (Friar) on Oct 17, 2008 at 16:52 UTC
    grep is the correct command, it just looks like your grep block needs tuning. If the company names are just strings, you ought to be able to use:
    @allcompanies = grep { not $_ eq $co } @allcompanies
    Notice if there are duplicates, this will whack all of them.
      While not $_ eq $co isn't incorrect, I rather write $_ ne $co.
      Notice if there are duplicates, this will whack all of them.
      That's easily solved:
      my $i = 0; @allcompanies = grep {$_ ne $co or $i++} @allcompanies;
        While not $_ eq $co isn't incorrect, I rather write $_ ne $co.

        I like yours better too.

        About duplicates: I assume whacking dups is actually what was intended based on the problem description, but I thought it would be worthwhile to mention that side-effect.

      works like a charm. guess that is why I should always have use warnings on, right? thanks again-
Re: splice help - deleting specific element
by Illuminatus (Curate) on Oct 17, 2008 at 16:49 UTC
    I think what you want is
    @allcompanies = grep {!/^$co$/} @allcompanies;
    Note that this is really no different than what you suggest (ie cycling), but it does, in fact, read much better...
      Using the match operator to compare strings exactly is overkill, and you got it wrong. I don't think many companies use regexp patterns for names.
      @allcompanies = grep {!/^\Q$co\E$/} @allcompanies;