Hi,

I have a array (@array) which holds around 2500 integer variables. Now, I want to add First 10 $array values ($array[0]+$array1+...+$array9) as first set. Then next 10 values ($array10+$array11+...+$array19) and so on.

For example. $Path/$Sample_ID.Count.txt file looks like this

Sample1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 .....

Sample2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 .....

Sample3 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 .....

So, I want; $Path/$Sample_ID.NewCount.txt, like this

Sample1 10 40 30 40 .....

Sample2 10 20 30 40 .....

Sample3 20 40 30 40 .....

I tried to write some code. But cant get the result. In fact I am getting the output similar to the Input file.

Please have a look. Thanks.

open (IN, "$Path/$Sample_ID.Count.txt") or die; open my $SCR_chr1, '>>', "$Path/$Sample_ID.NewCount.txt" or die "Canno +t create file for output: $!"; # go through the file line by line while (my $line = <IN>) { # split the current line on tabs chomp ($line); chop($line) if ($line =~ m/\r$/); my @columns = split(/\t/, $line); print $SCR_chr1 "$columns[0]"; #Will print the Sample +_ID in the #first column of t +he raw in new text file. for ($columns[0] =~ m/chr1$/) #if column[0] has speci +fic Sample ID { #and uses the ~2500 tab- +separated values in this raw for( my $i = 1; $i <= 250; $i++ ) { for( my $j; $j <= 10; $j++ ) ## I am hoping to parse +first 10 columns (2-11) { ## But I dont know how to us +e the Next 10 columns (12-21) in next run of for loop. my $Column_Value += $columns[$j]; ## And add these va +lue [first 10 columns (2-11)] to new variable } print $SCR_chr1 "\t$Column_Value "; ## and Print as new + column in the file } print $SCR_chr1 "\n"; break; } }

In reply to Nested for loop: Add arrays values in 1 set of 10 by amitgsir

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.