Hi
I have hashes with arrays to its keys like,
%h1 = ('a'=>['abc','def'],
'b'=>['ghi','jkl'],
'c'=>['hop','uio']
);
%h2 = ('a'=>['abc','def'],
'b'=>['wert','wer']);
then, how can i compare the 2 hashes(based on values. not keys) and get distinct values ?
Any solution will be appreciated.....
10 6517
The question is not very clear. I am not understanding with what criteria you want to compare the values of hashes(which are inturn arrays-compare lengths or elements of array??) . Also, let us know what have you tried so far.
However, to begin with if you are trying to get values of the hash, you may use 'values' keyword.
e.g. -
foreach(values %h1) {
-
print "@{$_}\n"; ## will print values of hash %h1(the array elements)
-
}
-
Thanks for responding.
i want to compare the values of %h1 with values of %h2 and the final result will be
( the values of %h1, which are in %h1 and not in %h2)
Result is :
['ghi' , 'jkl' ]
[ 'hop' ' uio']
(since, these are not in %h2).
I want the above output. How can i do this?????
Thanks for responding.
i want to compare the values of %h1 with values of %h2 and the final result will be
( the values of %h1, which are in %h1 and not in %h2)
Result is :
['ghi' , 'jkl' ]
[ 'hop' ' uio']
(since, these are not in %h2).
I want the above output. How can i do this?????
You can make use of Data::Compare module to compare data structures in perl. -
use Data::Compare;
-
use strict;
-
-
my %h1 = ('a'=>['abc','def'],
-
'b'=>['ghi','jkl'],
-
'c'=>['hop','uio']
-
);
-
my %h2 = ('a'=>['abc','def'],
-
'b'=>['wert','wer']);
-
-
foreach my $x (values %h1) {
-
my $i=0;
-
foreach my $y (values %h2) {
-
$i=1 if(Compare($x,$y));
-
}
-
print "@{$x}\n" if($i==0);
-
}
-
Thanks alot. This seems,it solves 99% of my problem.But
in my hashes are like,
my %h1 = ('a'=>['abc','def'],
'b'=>['ghi','jkl'],
'c'=>['hop','uio'],
'd'=>['iooo','qwe']
);
my %h2 = ('x'=>['abc','def'],
'b'=>['wert','wer']
'd'=>['iooo','qwe']
);
Even though, the values are same, I want the output as,
I want to match both keys and values.
So the Optput will come as, ['iooo','qwe'].
so, for your code, i add,
use Data::Compare;
use strict;
my %h1 = ('a'=>['abc','def'],
'b'=>['ghi','jkl'],
'c'=>['hop','uio'],
'd'=>['iooo','qwe']
);
my %h2 = ('x'=>['abc','def'],
'b'=>['wert','wer'],
'd'=>['iooo','qwe']
);
foreach my $x (values %h1)
{
my $i=0;
foreach my $y (values %h2)
{
$i=1 if(Compare($x,$y) && Compare($h1{$x},$h2{$y}) );
}
print "@{$x}\n" if($i==0);
}
but, This doesn't work.... Please help me out..... Thank you
first you said: how can i compare the 2 hashes(based on values. not keys)
now you say: I want to match both keys and values
which is it?
sorry for inconvenience.
As per my Hod Instructions and also regarding my application, ineed to do the aleternate post also..
Firstly I tried, but didn't get result.
How can I Solve this problem. Please help me out.
Thanks alot. This seems,it solves 99% of my problem.But
in my hashes are like,
my %h1 = ('a'=>['abc','def'],
'b'=>['ghi','jkl'],
'c'=>['hop','uio'],
'd'=>['iooo','qwe']
);
my %h2 = ('x'=>['abc','def'],
'b'=>['wert','wer']
'd'=>['iooo','qwe']
);
Even though, the values are same, I want the output as,
I want to match both keys and values.
So the Optput will come as, ['iooo','qwe'].
Your initial question is entirely different from your current requirement. However, you can make use of the same code and modify it to suit your requirement.
To display the value of the key-value pairs that are common in both hashes: -
use Data::Compare;
-
use strict;
-
-
my %h1 = ('a'=>['abc','def'],
-
'b'=>['ghi','jkl'],
-
'c'=>['hop','uio'],
-
'd'=>['iooo','qwe']
-
);
-
my %h2 = ('x'=>['abc','def'],
-
'b'=>['wert','wer'],
-
'd'=>['iooo','qwe']
-
);
-
-
foreach my $x (keys %h1) {
-
my $i=0;
-
foreach my $y (keys %h2) {
-
$i=1 if(Compare($x,$y) && Compare($h1{$x},$h2{$y}) );
-
}
-
print "@{$h1{$x}}\n" if($i==1);
-
}
-
-
Output will be:
Thanks alot nithinpes
Your Logic wroks well for me.
Thank alot.
Its great forum.
I tried like this.....................
while(my($k1,$v1) = each(%h1))
{
my $i = 0;
while(my($k2,$v2) = each(%h2))
{
$i = 1 if(Compare($v1,$v2) && Compare($k1,$k2));
}
print "@{$v1}\n" if($i==0);
}
This works fine and yours also works for me...............
Thank you............
Hello,
I have a different question on hash of arrays.
The programming language I am using is perl.
I have the following:
%a=(A1=>[UP1,UP3,UP6],
A2=>[UP1,UP6],
A3=>[UP2,UP10],
);
%b=(UP1=>[UP2,UP3,UP7],
UP2=>[UP10,UP9,UP3],
UP5=>[UP1,UP11],
);
Please keep in mind that the keys are different, so I can't compare them.
Desired output:
A1 has values UP1, UP3, UP6.
Among these, only UP1 is a key in %b.
The values of UP1 are UP2, UP3, UP7. I have to compare these 3 values with the values in %a. Whichever key in %a has any of these values, I have to print them.
Example: A1 has UP1, UP3, UP6. UP1 key has values UP2, UP3, UP7. Among these, UP2 has the key A3 and UP3 has the key A1.
So output is: A1-A3,A1.
I hope this explanation is clear. Please let me know if there are any doubts.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lance |
last post by:
is there a workaround for rows with null values not being returned? I am
using PHP 4.3.2 and mysql. the mysql_fetch_array just seems to ignore some
of the latest rows with null values.
|
by: Phil Powell |
last post by:
/*--------------------------------------------------------------------------------------------------------------------------------
Parameters:
$formField1: The name of the first array
$formField2:...
|
by: Gary |
last post by:
I've always taught that arrays are not pointers. So how come I can do:
#include <iostream>
#include <cstdlib>
using namespace std;
int main( )
{
int myArray={1, 10};
cout <<...
|
by: Fayez Al-Naddaf |
last post by:
I got this message when I tried to browse my web service
"Multi-dimensional arrays are not supported. Use a jagged
array instead"
Can someone told me why?
|
by: MFRASER |
last post by:
How can I compare two double values and return the greater of the two
values?
example
double doublea = 1.0;
double doubleb = 2.0
double retVal = ?compare doublea and doubleb
|
by: shocron |
last post by:
problem: input values not recognized in dinamicly loaded IFRAMEs
here is the thing I have a parent window that has an IFRAME
I then load a diffrent page into the IFRAME that contains an input
...
|
by: gits |
last post by:
This little article will show you how to optimize runtime performance when you need to compare two arrays (a quite common task). Have a close look at the entire article, and you will see the...
|
by: qwertz |
last post by:
Dear All;
I Am New To Perl Not So Experienced::::i Would Like To Know How I Can Compare Two Different Hashes And Print Thecommon Values In Them::it Would Be Great If Simeone Could Help Me
Thanks...
|
by: neha_chhatre |
last post by:
can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check
please help me as soon as possible
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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,...
| |