Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 ;).


Comment on Re: Adding a particular column from one file to a second file
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://914434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-12-23 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls