473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to get commen elements from multiple arrays

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

Please Help me.

Thanks in advance!

Expand|Select|Wrap|Line Numbers
  1. # To retrieve common elements from three array
  2. #!/usr/bin/perl
  3. use strict;
  4. use warnings;
  5. my @a1;   my @a2;    my @a3;
  6. my @data1 = ("sajan","ravi","arvind");  # Multiple arrays
  7. my @data2 = ("sajan","ravi","arjun");
  8. my @data3 = ("mahesh","raj","ravi");
  9. @a1 = sort {lc($a) cmp lc($b)} @data1;  # Sorting a to z
  10. @a2 = sort {lc($a) cmp lc($b)} @data2;
  11. @a3 = sort {lc($a) cmp lc($b)} @data3;
  12.  
  13. foreach my $a3 (my @a1, my @a2)          #compair @a3 array wirh @a1 & @a2
  14. {
  15.         if (my $a1 || my $a2 =~ my $a3)
  16.         {
  17.         print "Common element in all three array is: $a3";
  18.         }
  19. }
Aug 30 '10 #1
2 4772
RonB
589 Expert Mod 512MB
D:\perl>perldoc -q duplicate
Expand|Select|Wrap|Line Numbers
  1. Found in C:\Perl64\lib\pods\perlfaq4.pod
  2.   How can I remove duplicate elements from a list or array?
  3.     (contributed by brian d foy)
  4.  
  5.     Use a hash. When you think the words "unique" or "duplicated", think
  6.     "hash keys".
  7.  
  8.     If you don't care about the order of the elements, you could just create
  9.     the hash then extract the keys. It's not important how you create that
  10.     hash: just that you use "keys" to get the unique elements.
  11.  
  12.             my %hash   = map { $_, 1 } @array;
  13.             # or a hash slice: @hash{ @array } = ();
  14.             # or a foreach: $hash{$_} = 1 foreach ( @array );
  15.  
  16.             my @unique = keys %hash;
  17.  
  18.     If you want to use a module, try the "uniq" function from
  19.     "List::MoreUtils". In list context it returns the unique elements,
  20.     preserving their order in the list. In scalar context, it returns the
  21.     number of unique elements.
  22.  
  23.             use List::MoreUtils qw(uniq);
  24.  
  25.             my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7
  26.             my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7
  27.  
  28.     You can also go through each element and skip the ones you've seen
  29.     before. Use a hash to keep track. The first time the loop sees an
  30.     element, that element has no key in %Seen. The "next" statement creates
  31.     the key and immediately uses its value, which is "undef", so the loop
  32.     continues to the "push" and increments the value for that key. The next
  33.     time the loop sees that same element, its key exists in the hash *and*
  34.     the value for that key is true (since it's not 0 or "undef"), so the
  35.     next skips that iteration and the loop goes to the next element.
  36.  
  37.             my @unique = ();
  38.             my %seen   = ();
  39.  
  40.             foreach my $elem ( @array )
  41.                     {
  42.                     next if $seen{ $elem }++;
  43.                     push @unique, $elem;
  44.                     }
  45.  
  46.     You can write this more briefly using a grep, which does the same thing.
  47.  
  48.             my %seen = ();
  49.             my @unique = grep { ! $seen{ $_ }++ } @array;
  50.  
  51.  
Aug 30 '10 #2
numberwhun
3,509 Expert Mod 2GB
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
Aug 30 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James | last post by:
Hi, I have the following code: <PRE> <?php $username = "####t"; $password = "####"; $hostname = "####"; mysql_connect($hostname, $username, $password) or die("Unable to connect to
2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
0
by: raj | last post by:
Hey all I have a base class that manages (Manager_class) different shapes. It has different arrays of Circle_class, Square_class and etc etc. All the shapes have a common base class...
3
by: T | last post by:
Hi all. I have a problem I have not been able to find a reference about. I am using VB6 and am only a hobbyist programmer. I have 7 arrays of type MyData. Type MyData has 23 elements. Which...
0
by: Tom | last post by:
Shouldn't you be able to return them as 3 By Ref arguments in the function call? >-----Original Message----- >I have a class with a function which need to return to the caller three...
3
by: cs_hart | last post by:
I have the following data custID orderNum trackingNum ShipDate I have put these in multiple arrays since I want to redim. For example, the user selects one shipdate and I keep only the entries...
1
by: jpoloney | last post by:
I was wondering if there was a quick and easy way to sort multiple arrays in C++. What I mean is that, say I have 3 integer arrays. They are in order by array indices (array1 corresponds to array2...
1
by: tigerved | last post by:
Hi I am new to this so I dont exactly know the format in which to post the questions ....anyways here goes ...my file looks like this Year,Day,MOnth,latitude,longitude (commas included)...
1
by: BlackJackal | last post by:
Alright here is the problem I have for homework. I understand most of it but I am not exactly sure what the problem is asking me to do or how to search the seperate arrays using the account number...
1
by: kliopatraisis | last post by:
I think I've been working on this assignment for too long and my brain has stopped making connections! Basically, we are making a very simple version of AutoCAD, called HomeCAD. The problem I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.