Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How to I check if a value is present in an array?

by rocketperl (Sexton)
on Aug 03, 2013 at 10:46 UTC ( #1047693=perlquestion: print w/replies, xml ) Need Help??
rocketperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,, I am trying to iterate each element of one array to see if it is found in the other array and print only if it is found but i dont understand where i went wrong, all my array values are printing
do { if(grep($genes[$index],@gl)) { print O "$genes[$index]\n"; $index++; } else { $index++; } } until($index==@genes);

Replies are listed 'Best First'.
Re: How to I check if a value is present in an array?
by moritz (Cardinal) on Aug 03, 2013 at 10:54 UTC

    grep doesn't do what you think. It returns a list of all elements where the search expression is true. The search expression in your case is $genes[$index], so if that's a true value, grep returns all the elements of @gl.

    What you probably want to write instead is something like

    for my $current (@genes) { if (grep { $_ eq $current } @gl) { print $current; } }

    Though of course if either (or both) of these arrays is large, that is rather inefficient, and you might consider storing stuff in a hash in the first place.

      If @gl is (or can be) large/huge, List::Util's first can be an important speedup.

      use List::Util qw( first ); for my $current (@genes) { say $current if first { $_ eq $current } @gl; }

      Enjoy, Have FUN! H.Merijn
Re: How to I check if a value is present in an array?
by rjt (Deacon) on Aug 03, 2013 at 11:19 UTC

    This seems rather similar to how to refer the index of an array to another array of the same length you asked a little over an hour ago, which already has several replies.

    This one is a bit different, though, so here's what I would suggest. Since you will be looping over @gl for every item in @genes, put @gl in a hash (only the keys matter). You can use a hash slice:

        my %gl; @gl{@gl} = undef;

    Then, the lookup is both simple and efficient:

        say for grep { exists $gl{$_} } @genes;

    Complete example:

Re: How to I check if a value is present in an array?
by mtmcc (Hermit) on Aug 03, 2013 at 11:00 UTC
    Maybe something like this?:

    #!/usr/bin/perl use strict; use warnings; my @arrayONE = ("rabbits", "horses", "oranges"); my @arrayTWO = ("truck", "boat", "oranges"); for (my $x = 0; $x <= $#arrayONE; $x += 1) { for (@arrayTWO) { print STDERR "SHARED: $_\n" if $arrayONE[$x] eq "$_"; } }

Re: How to I check if a value is present in an array?
by tobyink (Abbot) on Aug 03, 2013 at 14:48 UTC

    Why do you persist in using two arrays when virtually all the answers in your previous thread (A better way to make the script run faster?) advised you to use a hash, which would make your script at least an order of magnitude faster.

    Anyway, you want something like:

    grep($genes[$index] eq $_, @gl)

    The first argument to grep is an expression or a block which is expected to return true or false.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: How to I check if a value is present in an array?
by Anonymous Monk on Aug 03, 2013 at 13:28 UTC

    Also, you might consider another type of data-structure, such as a hash, keyed by the value of the gene-string, whose value is the number of times that this particular string occurs.   This is remarkably easy to do in Perl:

    my $h={};
    $h->{ $_ } ++ foreach (@my_array);

    After initializing $h to an empty hashref, we loop over all elements in @my_array (each element being briefly represented by the variable $_), and increment the value of the hash-entry under that key.   Welcome to Perl’s “auto-vivication” feature:   each hash-element is magically created as necessary, and since you are “incrementing” it, the initial value automagically starts at zero.

    This representation is functionally-equivalent to an array of discrete strings, but much faster to process.

Re: How to I check if a value is present in an array?
by BillKSmith (Priest) on Aug 03, 2013 at 23:01 UTC
    Check the FAQ. perldoc -q "Intersection of two arrays"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047693]
Approved by rjt
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-05-28 17:56 GMT
Find Nodes?
    Voting Booth?