Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge lines and do operations if a condition is statisfied
    primarykey
    data
    text
    <p>I'm new in perl and I would like to read a table and make a sum of some values from specific lines. This is a simplified example of my input file:</p> <p>INPUT :</p> <pre><code>Gene Size Feature GeneA 1200 Intron 1 GeneB 100 Intron 1 GeneB 200 Intron 1 GeneB 150 Intron 2 GeneC 300 Intron 5 </code></pre> <p>OUTPUT : </p> <pre><code>GeneA 1200 Intron 1 GeneB 300 Intron 1 &lt;-- the size values are summed GeneB 150 Intron 2 GeneC 300 Intron 5 </code></pre> <hr> <p>Because Gene B is present for intron 1 with two different sizes, I would like to sum these two values and print only one line per intron number.</p> <p>This is an example of code that I want to do. But I would like to make it more complicated if I can understand How to handle this kind of data.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $sum; my @GAP_list; my $prevline = 'na'; open INFILE,"Table.csv"; while (my $ligne = &lt;INFILE&gt;) { chomp ($ligne); my @list = split /\t/, $ligne; my $gene= $list[0]; my $GAP_size= $list[2]; my $intron= $list[3]; my $intron_number=$list[4]; if($prevline eq 'na'){ push @GAP_list, $GAP_size; } elsif($prevline ne 'na') { my @list_p = split /\t/,$prevline; my $gene_p= $list_p[0]; my $GAP_size_p= $list_p[2]; my $intron_p= $list_p[3]; my $intron_number_p=$list_p[4]; if (($gene eq $gene_p) &amp;&amp; ($intron eq $intron_p) &amp;&amp; ($intron_number eq $intron_number_p)){ push @GAP_list, $GAP_size; } } else{ $sum = doSum(@GAP_list); print "$gene\tGAP\t$GAP_size\t$intron\t$intron_number\t$sum\n"; $prevline=$ligne; } } # Subroutine sub doSum { my $sum = 0; foreach my $x (@_) { $sum += $x; } return $sum; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload