amitgsir has asked for the wisdom of the Perl Monks concerning the following question:
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; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested for loop: Add arrays values in 1 set of 10
by Athanasius (Cardinal) on Aug 12, 2015 at 06:28 UTC | |
|
Re: Nested for loop: Add arrays values in 1 set of 10
by Monk::Thomas (Friar) on Aug 12, 2015 at 08:19 UTC | |
|
Re: Nested for loop: Add arrays values in 1 set of 10
by amitgsir (Novice) on Aug 12, 2015 at 07:12 UTC | |
by Laurent_R (Canon) on Aug 12, 2015 at 08:43 UTC |