in reply to to pick the common element in the array

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

Replies are listed 'Best First'.
Re^2: to pick the common element in the array
by Ksonar (Initiate) on Aug 28, 2017 at 07:48 UTC
    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
Re^2: to pick the common element in the array
by Anonymous Monk on Feb 20, 2014 at 18:51 UTC
    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
Re^2: to pick the common element in the array
by Anonymous Monk on Mar 13, 2009 at 12:48 UTC
    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