Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Adding a particular column from one file to a second file

by Not_a_Number (Parson)
on Jul 14, 2011 at 20:40 UTC ( #914434=note: print w/replies, xml ) Need Help??

in reply to Adding a particular column from one file to a second file

use strict; use warnings; my $file1 = "Wilder 1933 Col2 Col3 xyz Pitney 1940 Col2 Col3 xyz Hackman 1930 Col2 Col3 xyz Kelly 1912 Col2 Col3 xyz"; my $file2 = "Hackman Col2 Col3 xyz Kelly Col2 Col3 xyz Simmons Col2 Col3 zyx Wilder Col2 Col3 xyz"; open my $fh1, '<', \$file1; my %gene_dob; while ( my $line = <$fh1> ) { my ( $gene, $dob ) = ( split /\t/, $line )[0, 1]; $gene_dob{ $gene } = $dob; } close $fh1; open my $fh2, '<', \$file2; while ( my $line = <$fh2> ) { chomp $line; my $gene = ( split /\t/, $line )[0]; $line .= "\t$gene_dob{$gene}" if $gene_dob{ $gene }; print "$line\n"; }

Note that this code omits to issue any warning if any of your Genes are present in file1 but not in file2, or vice versa. Error checking is left as an exercise...

Update: Yeah, well. Marshall beat me to it by a couple of hours for the algorithm. My excuse: I was researching birth dates ;).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://914434]
[LanX]: enemies of the state !

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-05-25 01:19 GMT
Find Nodes?
    Voting Booth?