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
In reply to in_array and skipping foreach by webchalkboard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |