Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

in_array and skipping foreach

by webchalkboard (Scribe)
on Apr 13, 2005 at 14:28 UTC ( #447410=perlquestion: print w/ replies, xml ) Need Help??
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

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Comment on in_array and skipping foreach
Download Code
Replies are listed 'Best First'.
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

    First of all: You can use loop labels to controll which loop a next or last pertains to:

    OUTER: foreach my $item (@array){ foreach my $subitem (@array2){ next OUTER if($subitem eq $item); } }

    But to actually answer your question: I think it would be better to use grep in a scalar context to test for the presence of an item in an array (as opposed to foreach'ing through the array doing an eq comparison).

    foreach my $item (@array){ next if(scalar(grep({$_ eq $item} @other_array))); }

    see grep, next and last

    HTH. Cheers

    Update: As others have suggested, a hash is better for this type of lookup


    A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

    —His Holiness, The Dalai Lama

Re: in_array and skipping foreach
by Random_Walk (Parson) on Apr 13, 2005 at 14:35 UTC

    Put the ones to skip into a Hash not an array

    my %skip; $skip{$_}++ for qw(skip_me and_me and_this_too); foreach my $line (@csv) { if ($csv->parse($line)) { my @fields = $csv->fields; $offer{retailer}=$fields[0]; next if $skip{$offer{retailer}}; $offer{productid}=$fields[1]; } }

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: in_array and skipping foreach
by webchalkboard (Scribe) on Apr 13, 2005 at 14:42 UTC

    Doh I feel stupid, and I love hashes :) definately the way to go.

    Thanks for the info about the next labels too, I will check that out now.

    Learning without thought is labor lost; thought without learning is perilous. - Confucius
    WebChalkboard.com | For the love of art...
Re: in_array and skipping foreach
by RazorbladeBidet (Friar) on Apr 13, 2005 at 14:31 UTC
    There's no specific function for finding a value in an array. You make your own (or use a module). Yours seems fine.

    For your second question, see next - specifically "next LABEL"
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
        Ah yes, I spoke to soon. Shouldn't write answers when distracted ;)
        --------------
        "But what of all those sweet words you spoke in private?"
        "Oh that's just what we call pillow talk, baby, that's all."
        Isn't it faster to create a lookuptable next to your array? The value in the array will be the key in the lookup table (hash) with the value 0 or 1.

        "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://447410]
Approved by ghenry
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2015-07-29 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls