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!

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.