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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|