 |
|
Home:
Perl Programming Help:
Beginner:
Re: [manchester] Extracting Data from a File and Tabulating It :
Edit Log
|
|

Chris Charley
User
Feb 28, 2013, 3:57 PM
Views: 194
|
|
Re: [manchester] Extracting Data from a File and Tabulating It
|
|
|
I 'cleaned up' your code - there was only 1 obvious error I saw. my %microRNA_read_count_3 = ( ); That line wipes out your hash every time through the loop and you will only get the last record read. Just eliminate that line. Instead of using for loops, it is better to use a while loop. See your code below with those changes. Just to repeat what Bill Smith said, you should probably not use 4 hashes - just 1 and possibly use an array as he described. #!/usr/bin/perl use strict; use warnings; my %microRNA_read_count_1 = ( ); my %microRNA_read_count_2 = ( ); my %microRNA_read_count_3 = ( ); my %microRNA_read_count_4 = ( ); open(INFILE,$ARGV[0]) or die $!;; while (<INFILE>){ chomp; my @row = split /\t/; my $mir = $row[0]; my $count = $row[1]; $microRNA_read_count_1{$mir} = $count; } close INFILE or die $!; open(INFILE,$ARGV[1]) or die $!; while (<INFILE>){ chomp; my @row = split /\t/; my $mir = $row[0]; my $count = $row[1]; $microRNA_read_count_2{$mir} = $count; } close INFILE or die $!; open(INFILE,$ARGV[2]) or die $!; while (<INFILE>){ chomp; my @row = split /\t/; my $mir = $row[0]; my $count = $row[1]; $microRNA_read_count_3{$mir} = $count; } close INFILE or die $!; open(INFILE,$ARGV[3]) or die $!; while (<INFILE>){ chomp; my @row = split /\t/; my $mir = $row[0]; my $count = $row[1]; $microRNA_read_count_4{$mir} = $count; } close INFILE or die $!; open(INFILE,$ARGV[4]) or die $!; while (<INFILE>){ # Reads the input file line by line chomp; #print $_ ."\t". $microRNA_read_count_1{$_} ."\t". $microRNA_read_count_2{$_} ."\t". $microRNA_read_count_3{$_} ."\t". $microRNA_read_count_4{$_} ."\n"; print join("\t", $_, $microRNA_read_count_1{$_} || 0, $microRNA_read_count_2{$_} || 0, $microRNA_read_count_3{$_} || 0, $microRNA_read_count_4{$_} || 0), "\n"; } close INFILE or die $!; The lines like $microRNA_read_count_2{$_} || 0 assign a zero, 0, when that name was not found in the hash you created.
(This post was edited by Chris Charley on Feb 28, 2013, 8:07 PM)
|
|
|
Edit Log:
|
|
Post edited by Chris Charley
(User) on Feb 28, 2013, 3:58 PM
|
|
Post edited by Chris Charley
(User) on Feb 28, 2013, 8:07 PM
|
|
|  |