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

to pick the common element in the array

by soubalaji (Sexton)
on Oct 17, 2008 at 13:22 UTC ( #717727=perlquestion: print w/replies, xml ) Need Help??

soubalaji has asked for the wisdom of the Perl Monks concerning the following question:

@array1=('a',''2,'3','2');
@array2=('1','2,'3','2');
We want to pick the common element from the above two array.
there is any method to do this?
Regards,
Balaji S
  • Comment on to pick the common element in the array

Replies are listed 'Best First'.
Re: to pick the common element in the array
by dorko (Prior) on Oct 17, 2008 at 13:28 UTC
    Personally, I like List::Compare:
    use strict; use warnings; use List::Compare; use Data::Dumper; my @array1=('a','2','3','2'); my @array2=('1','2','3','2'); my $lc = List::Compare->new(\@array1, \@array2); # Get those items which appear at least once in both lists (their inte +rsection). my @intersection = $lc->get_intersection; print Dumper \@intersection;
    Also, use strict; and use warnings; caught the typos you made in defining the arrays.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: to pick the common element in the array
by zentara (Archbishop) on Oct 17, 2008 at 14:12 UTC
    Looking for something like this?
    #!/usr/bin/perl use warnings; use strict; my @array1=('a','2','3','2'); my @array2=('1','2','3','2'); my %in_array2 = map { $_ => 1 } @array2; my @array3 = grep { $in_array2{$_} } @array1; print "common @array3\n"; my %seen; my @unique = grep{!$seen{$_}++} @array3; print "unique @unique\n";
    (code can probably be golfed down.) :-)

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Hi,

      I am trying to something similar but not really...

      there are three arrays:

      @array1 = ('Leishmania infantum','Leishmania donovani','Leishmania pan +amensis','Leishmania braziliensis', 'Leishmania guyanensis','Leptomon +as seymouri','Trypanosoma cruzi','Trypanosoma grayi'); @array2 = ('lin','ldo','lpa','lbr','lgu','lse','tcr','tgr'); @array3 = ('Leishmania infantum JPCM5','Leishmania infantum JPCM5','Le +ishmania donovani','Leishmania donovani','Leishmania panamensis','Lei +shmania braziliensis MHOM/BR/75/M2904','Leishmania panamensis','Leish +mania braziliensis MHOM/BR/75/M2904','Leishmania guyanensis','Leptomo +nas seymouri','Trypanosoma cruzi marinkellei','Trypanosoma cruzi','Tr +ypanosoma cruzi strain CL Brener','Trypanosoma rangeli SC58','Trypano +soma cruzi','Trypanosoma cruzi strain CL Brener','Trypanosoma cruzi D +m28c','Trypanosoma cruzi','Trypanosoma theileri','Trypanosoma grayi', +'Trypanosoma grayi');
      basically I want to match array3 with array1 and then using the index position of array1, I want to pick the elements from array2.

      I have used grep,~~,index, everything, but no success.

      I would be great if you can help me.

        Hi, Ksonar. You should probably post this as a new node, and put code tags around your code example.

        I'm not really a human, but I play one on earth. ..... an animated JAPH
      does thie code work for common elements when the following two arrays are used? my @array1=('a','2','3','2'); my @array2=('1','2','3');
        What happens when you try?

        Perl is good about doing what you mean. Usually, languages will separate ascii data from numerical data, it's called "context". The code I showed treated alphanumeric strings and numbers exactly the same. So it should work.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
      I am new to perl can u explain the above code... Thanks
        I'm new to being a free tutor, get a tutorial on arrays and hashes. :-) Seriously, it is some advanced code work ( which by the way, I did not write....it's been passed around in books and tutorials for a long time. The general idea is to check all array elements...dump them in a hash for counting, then check the hash keys (an array) for 1's or 0's....(or something along those general lines).

        Probably every programming language has some sort of "array comparison" abilities... google for "perl array comparison", grasshopper, and when you are done reading all 5 million hits....come back and teach me. :-)


        I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: to pick the common element in the array
by Fletch (Chancellor) on Oct 17, 2008 at 13:30 UTC

    It's 2. HTH. HAND. BMSMA.

    Of course if that's not what you had in mind you should probably read How (Not) To Ask A Question, and see perlfaq4 for the question "How do I compute the difference of two arrays? How do I compute the intersection of two arrays?".

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Was this necessary? I understood him quite easily.
Re: to pick the common element in the array
by JavaFan (Canon) on Oct 17, 2008 at 13:53 UTC
    Actually, there's no method to do this, because the problem isn't well defined. You're asking for the common element, but there are at least two common elements, three if you consider the two 2's in each array to be distinct.
Re: to pick the common element in the array
by UnderMine (Friar) on Oct 17, 2008 at 13:32 UTC
    Is position as well as value important?
    i.e. from you example would you expect back @answer=(2,3); or @answer=(undef,2,3,2);
    UnderMine

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2019-09-17 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (207 votes). Check out past polls.

    Notices?