How to get commen elements from multiple arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ravi More
    New Member
    • Aug 2010
    • 1

    #1

    How to get commen elements from multiple arrays

    Hello, I want to get comment element from three arrays.

    Please Help me.

    Thanks in advance!

    Code:
    # To retrieve common elements from three array
    #!/usr/bin/perl
    use strict;
    use warnings;
    my @a1;   my @a2;    my @a3;
    my @data1 = ("sajan","ravi","arvind");  # Multiple arrays
    my @data2 = ("sajan","ravi","arjun");
    my @data3 = ("mahesh","raj","ravi");
    @a1 = sort {lc($a) cmp lc($b)} @data1;  # Sorting a to z
    @a2 = sort {lc($a) cmp lc($b)} @data2;
    @a3 = sort {lc($a) cmp lc($b)} @data3;
    
    foreach my $a3 (my @a1, my @a2)          #compair @a3 array wirh @a1 & @a2
    {
            if (my $a1 || my $a2 =~ my $a3)
            {
            print "Common element in all three array is: $a3";
            }
    }
    Last edited by Frinavale; Aug 31 '10, 01:56 PM. Reason: Added the question/reason for posting to the thread. Please post your question in thraed in the future.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    D:\perl>perldoc -q duplicate
    Code:
    Found in C:\Perl64\lib\pods\perlfaq4.pod
      How can I remove duplicate elements from a list or array?
        (contributed by brian d foy)
    
        Use a hash. When you think the words "unique" or "duplicated", think
        "hash keys".
    
        If you don't care about the order of the elements, you could just create
        the hash then extract the keys. It's not important how you create that
        hash: just that you use "keys" to get the unique elements.
    
                my %hash   = map { $_, 1 } @array;
                # or a hash slice: @hash{ @array } = ();
                # or a foreach: $hash{$_} = 1 foreach ( @array );
    
                my @unique = keys %hash;
    
        If you want to use a module, try the "uniq" function from
        "List::MoreUtils". In list context it returns the unique elements,
        preserving their order in the list. In scalar context, it returns the
        number of unique elements.
    
                use List::MoreUtils qw(uniq);
    
                my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7
                my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7
    
        You can also go through each element and skip the ones you've seen
        before. Use a hash to keep track. The first time the loop sees an
        element, that element has no key in %Seen. The "next" statement creates
        the key and immediately uses its value, which is "undef", so the loop
        continues to the "push" and increments the value for that key. The next
        time the loop sees that same element, its key exists in the hash *and*
        the value for that key is true (since it's not 0 or "undef"), so the
        next skips that iteration and the loop goes to the next element.
    
                my @unique = ();
                my %seen   = ();
    
                foreach my $elem ( @array )
                        {
                        next if $seen{ $elem }++;
                        push @unique, $elem;
                        }
    
        You can write this more briefly using a grep, which does the same thing.
    
                my %seen = ();
                my @unique = grep { ! $seen{ $_ }++ } @array;

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      The next time you post code in the forums, you need to please surround it with code tags.

      Also, you should really expand upon just posting code and try telling us what is going wrong or not happening in your code. You cannot just expect us to run your code and figure it out. We would really like some kind of explanation of what you are expecting what you are seeing and what is going wrong or not happening.

      Regards,

      Jeff

      Comment

      Working...