it goes wrong on the very last line of the file

Between the 2 foreach blocks, add these 2 lines of code.

$pval{$prev_join} = $prev_pval/$i; $fst{$prev_join} = $prev_fst/$i;

There were alot of warnings about some of your variables not being initialized. To get rid of the warnings, I initialized them with values from the first line in the file. Posting some of that code here.

open INFILE, "<$infile" or die $!; chomp(my @lines = <INFILE>); close INFILE or die $!; my ($chr, $prev_loc, $prev_pval, $prev_fst) = (split /\t/, $lines[0])[ +1,2,5,6]; my $prev_join = "$chr-$prev_loc"; my $i=1; my %pval; my %fst; foreach (@lines[1 .. $#lines]){ my ($name, $chr, $location, $gen, $dom, $pval, $fst) = split /\t/; my $join = "$chr-$location"; if ($location == $prev_loc){ $pval = $pval + $prev_pval; $fst = $fst + $prev_fst; $i++; } else { $pval{$prev_join} = $prev_pval / $i; $fst{$prev_join} = $prev_fst / $i; $i = 1; } $prev_loc = $location; $prev_pval = $pval; $prev_fst = $fst; $prev_join = $join; } $pval{$prev_join} = $prev_pval/$i; $fst{$prev_join} = $prev_fst/$i; foreach (@lines){ my ($name, $chr, $location, $gen, $dom, $pval, $fst) = split /\t+/ +; my $join = "$chr-$location"; my $new_pval = $pval{$join}; my $new_fst = $fst{$join}; print $name."\t".$chr."\t".$location."\t".$gen."\t".$dom."\t".$new +_pval."\t".$new_fst."\n"; }

There are 2 different criteria about what you use to create a group to average. In your code, a group to average is determined by the code, if ($location == $prev_loc) .... But you are also joining the $chr and $location keys for the 2 different hashes here. I believe this leaves the door open for difficult to trace behaviors. It works without error with the sample data provided, but it could break down with an actual file.

Would you want a line like if ($join eq $prev_join)...?

Why join the chr location columns. Can the same location have different 'chr' columns? If not, there is no advantage to combining them like that. They could be more simply using the location for the 2 hashes here and the keys are simple to sort, the keys being 'location' alone - a floating point number.


In reply to Re^5: average a column in tab-delimited file by Cristoforo
in thread average a column in tab-delimited file by garyboyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.