CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [Thalakos] Merges 2 text files under few conditions : Edit Log

Chris Charley

Apr 5, 2013, 5:50 PM

Views: 1046
Re: [Thalakos] Merges 2 text files under few conditions

Yes, you are correct - the code I posted doesn't produce the correct results. In addition, one of your lines in file_b, 'hsa-let-7d BDNF , D3R' had a space AND a tab, so it didn't parse correctly. When the space was eliminated leaving the desired tab, it parsed correcly. I'm attaching the result of the run, (o33.txt) and a reconciliation, (file_c.txt), which proves the code now produces the desired results.

use strict;
use warnings;

my %ID;

open my $a_fh, '<', 'file_a.txt' or die "failed to open file_A.txt $!";
open my $b_fh, '<', 'file_b.txt' or die "failed to open file_B.txt $!";

while ( my $id = <$a_fh> ) {
chomp $id;
close $a_fh or die $!;

my %seen;
while ( my $line = <$b_fh> ) {
my($id, $genes) = split /\t/, $line;

if (exists $ID{$id}) {
print $line x $ID{$id};
else {
print $line;
close $b_fh or die $!;

delete @ID{ keys %seen };

print "$_\n" x $ID{$_} for keys %ID;

You must be sure that every line is tab separated, (in file_b), or you won't get accurate results. If you can't be sure, then maybe split on space instead of tabs.

 my($id, $genes) = split /\s+/, $line, 2;

(This post was edited by Chris Charley on Apr 5, 2013, 5:52 PM)
Attachments: o33.txt (6.78 KB)
  file_c.txt (0.12 KB)

Edit Log:
Post edited by Chris Charley (User) on Apr 5, 2013, 5:52 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives