Hello, I want to get comment element from three arrays.
Please Help me.
Thanks in advance! - # 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";
-
}
-
}
2 4772 RonB 589
Expert Mod 512MB
D:\perl> perldoc -q duplicate - 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;
-
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |