473,748 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare 2 hashes of arrays by values(not keys)

34 New Member
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.... .
Aug 27 '08 #1
10 6556
nithinpes
410 Recognized Expert Contributor
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.
Expand|Select|Wrap|Line Numbers
  1.  foreach(values %h1) {
  2.   print "@{$_}\n";   ## will print values of hash %h1(the array elements)
  3. }
  4.  
Aug 27 '08 #2
aurekha
34 New Member
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?????
Aug 27 '08 #3
nithinpes
410 Recognized Expert Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. use Data::Compare;
  2. use strict;
  3.  
  4. my %h1 = ('a'=>['abc','def'],
  5. 'b'=>['ghi','jkl'],
  6. 'c'=>['hop','uio']
  7. );
  8. my %h2 = ('a'=>['abc','def'],
  9. 'b'=>['wert','wer']);
  10.  
  11. foreach my $x (values %h1) {
  12.   my $i=0;
  13.  foreach my $y (values %h2) {
  14.   $i=1 if(Compare($x,$y));
  15. }
  16.   print "@{$x}\n" if($i==0);
  17. }
  18.  
Aug 27 '08 #4
aurekha
34 New Member
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
Aug 28 '08 #5
KevinADC
4,059 Recognized Expert Specialist
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?
Aug 28 '08 #6
aurekha
34 New Member
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.
Aug 28 '08 #7
nithinpes
410 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. use Data::Compare;
  2. use strict;
  3.  
  4. my %h1 = ('a'=>['abc','def'],
  5. 'b'=>['ghi','jkl'],
  6. 'c'=>['hop','uio'],
  7. 'd'=>['iooo','qwe']
  8. );
  9. my %h2 = ('x'=>['abc','def'],
  10. 'b'=>['wert','wer'],
  11. 'd'=>['iooo','qwe']
  12. );
  13.  
  14. foreach my $x (keys %h1) {
  15.   my $i=0;
  16.  foreach my $y (keys %h2) {
  17.   $i=1 if(Compare($x,$y) && Compare($h1{$x},$h2{$y}) );
  18. }
  19.   print "@{$h1{$x}}\n" if($i==1);
  20. }
  21.  
  22.  
Output will be:
Expand|Select|Wrap|Line Numbers
  1.  iooo qwe 
  2.  
  3.  
Aug 28 '08 #8
aurekha
34 New Member
Thanks alot nithinpes

Your Logic wroks well for me.


Thank alot.


Its great forum.
Aug 28 '08 #9
aurekha
34 New Member
I tried like this........... ..........

while(my($k1,$v 1) = each(%h1))
{
my $i = 0;
while(my($k2,$v 2) = 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............
Aug 28 '08 #10

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

Similar topics

2
2850
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.
0
2711
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2: The name of the second array $formField1CompareWith: String to use as my comparison basis for first array. Defaults to using $val unless it's 'key' $formField2CompareWith: String to use as my comparison basis for second array. Same default as...
18
1365
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 << sizeof(myArray) << endl;
1
6299
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?
4
5165
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
7
2245
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 when I try to access - iFrm.document.all.value i get an error saying that - "value is null or not an object" I tryed checking to make sure the page is fully loaded using:
0
38211
gits
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 advantage of the final code snippet :) Let's assume we want to find out the element that is contained by both of the lists. var list1 = ; var list2 = ; for (var i in list1) { for (var j in list2) {
1
1848
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 In Advance
26
7137
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
0
8994
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9250
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.