We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,524 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Comparing and save similar data between two files

Hi!

I'm working on a Perl code since two weeks without found anything so, I'm asking for your help.

I have two files:

The first is like this:

141374_at	AttA	
141559_at	sec5	
141691_at	spz	
141930_at	imd

These datas were selected before according to a criteria.

And the second is like that:

153814_at	0.09276438
153815_at	0.28571427
153816_at	0.63358074
153817_at	4.305195
153818_at	0.15955473
153819_at	0.40723562

These datas are not treated but have an interesting information in column 2.

So, I would like to get a new file with columns 1 and 2 from the first file and column 3 of file 2 which correspond to datas from column 1 of file 1.

I hope to put out:

141374_at	AttA	1.671614
141559_at	sec5	0.4499072
141691_at	spz	0.4638219
141930_at	imd	0.110389605

I think about hashes but I'm not familiar with.

Thanks for any help,
Perlie

Sorry for my English, I'm a French Perl user!

2
Contributors
2
Replies
18 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Perlie
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You can read the second file into a hash if the values in the first column are unique. By 'unique' I mean that '153814_at' occurs no more than once in the second file, so you can use it as a key in your hash whose value is 0.09276438. After building your hash by reading through your second file you can open your first file. For each record in your first file you can split the record into its two columns and see if an entry corresponding to the value in the first column occurs in the hash. If it does you can take the value associated with that key from the hash and you have the three data items that you want to print.

For example:
file1.txt

beans   green
lime    green
banana  yellow
carrot  orange

file2.txt

apple   fruit
carrot  vegetable
beans   vegetable
banana  fruit
spinach vegetable

script.pl

#!/usr/bin/perl
use strict; 
use warnings; 

my $filename1 = 'file1.txt';
my $filename2 = 'file2.txt';
my %categories;#Hash to store key=>value pairs from file2.txt

open my $fh, '<', $filename2 or die "Failed to open $filename2: $!";
while (<$fh>){
    chomp;
    my ($name, $cat) = split;
    $categories{$name} = $cat;
}
close $fh;

open $fh, '<', $filename1 or die "Failed to open $filename1: $!";
while (<$fh>){
    chomp;
    my ($name, $colour) = split;
    my $cat;
    
    if (exists $categories{$name}){
        $cat = $categories{$name}
    }
    else{
        $cat = "***$name Not Found in $filename2***";
    }
    print "$name\t$colour\t$cat\n";
}

close $fh;

Output:

beans	green	vegetable
lime	green	***lime Not Found in file2.txt***
banana	yellow	fruit
carrot	orange	vegetable
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1
Question Answered as of 1 Year Ago by d5e5

Thank you very much d5e5! That's help me a lot!

Perlie
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.5682 seconds using 2.72MB