de2425, this is a fast and dirty rewrite of your code, incorporating some of
moritz's and
shmem's comments. Further, I'll note you don't get keys from $ING, but rather from the hash %ING. The code below has not been run but it has been checked for syntax with
perl -cw. I'll note that most of what I've done is clean up the variable syntax (and I'm sure someone else could do a better job of it).
I've also taken the time to eliminate a lot of those hard coded files and directory settings. The name of the output file can now be passed as a parameter as well.
#!/usr/bin/perl
use warnings;
use strict;
#
# Rewrite for legibility.
# This code is not tested.
#
my $output_file = shift || "Cytokine.txt";
my $work_dir = "c:/work/Cytokine_By_Company";
my $report_dir = "c:/work/GeneID_Count";
my $data_set_one = "ING_cytokines_200805.txt";
my $data_set_two = "CytokineArrays.txt";
my %ING;
open (IN, "< $work_dir/$data_set_one") or
die "Could not open input file $data_set_one. $!\n";
while (<IN>) {
chomp;
my @source_data = split(/\t/,$_);
$ING{$source_data[8]}{name} = $source_data[0];
$ING{$source_data[8]}{count} = 0;
}
close IN;
open (OUT, "> $report_dir/$output_file") or
die "Could not open file $output_file. $!\n";
open (IN, "< $work_dir/$data_set_two") or
die "Could not open file $data_set_two. $!\n";
while(<IN>) {
chomp;
my @target_data = split(/\t/,$_);
my $cytokine_id = $target_data[2];
while (/\d+/ and exists $ING{$cytokine_id}{name}) {
$ING{$cytokine_id}{count}++;
}
for my $id ( sort{ $ING{$b} <=> $ING{$a} } keys %ING) {
print OUT "$ING{$id}{name}\t$ING{$id}{count}\n";
}
}
close IN;
close OUT;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.