webchalkboard has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Just wondering how can I check if a value exists in an array in Perl? Then if it does, just skip to the next element...
In PHP you can use in_array or something, here is how I would write it long-hand in perl, but there must be a more succinct version as there always is in Perl ;) I don't even think this version will work exactly, as I want the 'next' statement to go to the next value in the outer foreach loop, rather than the one it is in.
use Text::CSV_XS; my $csv = Text::CSV_XS->new({ 'quote_char' => '"', 'escape_char' => '"', 'sep_char' => ',', 'binary' => 1 }); open FH, "< textfile.txt" or die "Failed to open CSV file for processi +ng: $!"; my @csv=(<FH>); close(FH) or die "Failed to close CSV file for processing: $!"; my @ignore_merchants=('merchant 1','merchant 2'); foreach my $line (@csv) { if ($csv->parse($line)) { my @fields = $csv->fields; $offer{retailer}=$fields[0]; $offer{productid}=$fields[1]; foreach (@ignore_merchants) { # now I want to skip to the next element in the outer loop +. How can I do that anyway? next if ($_ eq $offer{retailer}); } }
Any ideas how I can make this better?
Cheers,
Tom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: in_array and skipping foreach
by jdporter (Paladin) on Jan 16, 2020 at 16:34 UTC | |
|
Re: in_array and skipping foreach
by Joost (Canon) on Apr 13, 2005 at 14:34 UTC | |
|
Re: in_array and skipping foreach
by JediWizard (Deacon) on Apr 13, 2005 at 14:34 UTC | |
|
Re: in_array and skipping foreach
by Random_Walk (Prior) on Apr 13, 2005 at 14:35 UTC | |
|
Re: in_array and skipping foreach
by webchalkboard (Scribe) on Apr 13, 2005 at 14:42 UTC | |
|
Re: in_array and skipping foreach
by crashulater (Initiate) on Jan 16, 2020 at 16:21 UTC | |
|
Re: in_array and skipping foreach
by RazorbladeBidet (Friar) on Apr 13, 2005 at 14:31 UTC | |
by VSarkiss (Monsignor) on Apr 13, 2005 at 14:34 UTC | |
by jbrugger (Parson) on Apr 13, 2005 at 14:43 UTC | |
by VSarkiss (Monsignor) on Apr 13, 2005 at 14:59 UTC | |
by RazorbladeBidet (Friar) on Apr 13, 2005 at 14:36 UTC |