473,770 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

perl hash doubt

89 New Member
Hi,
I have two text file as below:

file1.txt (4 columns)
Expand|Select|Wrap|Line Numbers
  1. test1 1000 2000 +
  2. test2 1000 2000 -
  3. test1 1000 2000 +
  4. test3 1000 2000 +
  5. test1 1000 2000 -
  6. test2 2000 3000 +
  7. test1 1000 2000 +
  8. test1 1000 3000 -
  9.  
file2.txt also contains very similar data.

The first step in the processing is that I want to collect all the data. Hence I want to use column1 as key and column2,3 and 4 as values.

For all test1, all other three columns will be added after removing duplicates. if two lines as below are available, then only one value should be added.
Expand|Select|Wrap|Line Numbers
  1. test1 1000 2000 -
  2. test1 1000 2000 -
  3.  
key = {test1} and value = {1000 2000 -}
Since I have always used perl hash with two columns, the first column as key and second into hash and at the same time adding them in perl hash helped me to get rid of duplicate values of the second column, I want to know how that can be applicable for the above dataset. column 1 as key and column 2,3and4 as values (basically to remove duplicates) Can I cat them into one string with some pattern as
"1000:2000:-". Please let me know.

My second query would be how to compare perl hash1 (data from file1) and hash2(data from file2).

For eaxmple for test1 (every key), I have to compare the values between two hashes.

Please let me know as I am not familiar with perl hash of hash. Do I have to use that?

My basic motivation is to remove duplicates from two files and then compare two hashes to find how many of the column2:column2 :column3 are present in both files as well the ones that are unique to each data set. Or any other way to handle the data?

And it is highly confusing. A small example woule be easy for me to proceed.

Thanks in advance.
Oct 27 '09 #1
4 2603
toolic
70 Recognized Expert New Member
I do not understand what you are trying to accomplish.

If you also post a small example of your 2nd file, along
with the exact output you are looking for, I might be able to
create some example code for you.

Hash-of-hashes data structure are very useful
and may be appropriate for this task.
Oct 27 '09 #2
lilly07
89 New Member
Hi Toolic thx for your response. my file2.txt also contains the data in the same format as file1.txt

Expand|Select|Wrap|Line Numbers
  1. test1 1000 4000 + 
  2. test3 1000 2000 - 
  3. test1 1000 2000 + 
  4. test5 5000 7000 + 
  5. test1 1000 2000 - 
  6. test2 2000 4000 + 
  7. test3 1000 6000 + 
  8. test1 1000 3000 - 
  9.  
As I mentioned earlier, I need to collect them as test1 (column1) and column2, 3 and 4 has to be collected as values after removing the duplicates. For example
iif there are two records as below:
Expand|Select|Wrap|Line Numbers
  1. test1 1000 2000 - 
  2. test1 1000 2000 - 
  3.  
Then only one pair should be added into hash as test1 {key} and "1000 2000 - " as {value}. This resolves records removing the duplicates of cloumn2,3 and 4.

And the next step would be comparing two files.

As each file1 and file2 data are collected in two different hashes, I want to compare for every (key) in file2 (ie test1), I want to compare each value in file2 hash exists in file2(same key (ie test1) whether value exists or not. Same key between two hashes are selected and values among both hashes are to be compared. Is it feasible?
I know this is highly confusing. Sorry and thanks again.

Regards
Oct 28 '09 #3
nithinpes
410 Recognized Expert Contributor
The approach would be to remove duplicate lines first.
Then split each unique line on spaces into an array and then create a hash of arrays. The first column in the file will be made hash key and the remaining values will be pushed into an array which is value for the key. Whenever you come across a key that already exists, append rest of the elements into the existing value of key.
I am giving only the approach since you haven't posted the code that you tried. If you face any issues, post the code that you tried so that we can correct or modify the code.
Oct 29 '09 #4
lilly07
89 New Member
Yes. Thank you so much for your response.

I managed to remove duplicates as below. A very simple way.

Expand|Select|Wrap|Line Numbers
  1.  %seen = ();
  2. while(<>){
  3.       $seen{$_}++;
  4.       next if $seen{$_} > 1;
  5.       print "$_";
  6.    }
As I don't know how to compare between two hashes, I couldn't proceed further. I will definitely follow pushing the data in to perl key and array as you suggested. Thanks.
Oct 30 '09 #5

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

Similar topics

5
5772
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an object that contains a hash of sub-objects each sub-object is made up of a number of different objects and an array of an object type. Object1 contains scalars
5
3229
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: someArray = 6; I know I can't get the same syntactic sugar as Perl offers, with the usage of a string as the array key surrounded by square brackets. I just want the general functionality, that's all. That is, a data container that will maintain...
0
9747
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
10
2455
by: Qiangning Hong | last post by:
I'm writing a spider. I have millions of urls in a table (mysql) to check if a url has already been fetched. To check fast, I am considering to add a "hash" column in the table, make it a unique key, and use the following sql statement: insert ignore into urls (url, hash) values (newurl, hash_of_newurl) to add new url. I believe this will be faster than making the "url" column unique key and doing string comparation. Right?
3
4707
crazy4perl
by: crazy4perl | last post by:
Hi All, I have some doubt related to xml. Actually I want to update a file which is in some format. So I am converting that file using Tap3edit perl module in a hash. Now I m trying to create a XML file using this hash so that I can edit that file and convert it back to the same format (might be there are some better way to edit... any suggestions are welcome). As there are so many xml modules I m not sure which one will be the best n...
11
3203
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side programming since I am in the field of web design, development and multimedia. Here are some tips and need some help how to program: The way I learn is that I need an detailed explanation why and how we need use certain functions in PERL: I...
1
2974
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my $i=0; $i<@input; $i++){ my $hash = $input; print "$i: \n";
5
2100
by: lilly07 | last post by:
Hi, I have a doubt regarding perl hash data. I have a tab delimited as below: file1.txt name1 AM bin1 name2 AM bin1 name3 PM bin1
0
9618
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
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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
6712
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
5354
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...
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.