Hello Monks. I am back with more novice questions! I have a list of alleles that come in non-unique blocks e.g. there might be 7 occurrences of HLA-A*02:01, followed by 3 occurrences of HLA-A*03:01, followed by another 7 occurrences of HLA-A*02:01 and finally 8 occurrences of HLA-A*02:03 (see my DATA below). I want to count these non unique groups so that I would get an array of the counts (7,3,7,8). I have almost? cracked the problem but I cannot get the last count to (e.g. in this example = 8) into my array
my $newcount = 0; my $totdupAlleles = scalar (@allele); my @newcount; for (my $i=0;$i<$totdupAlleles-1;$i++){ if ($allele[$i] eq $allele[$i+1]){ $newcount++; } # if ($allele[$i] ne $allele[$i+1] or $i = $totdupAlleles-1){ # alt +ernative line to try and capture last count if ($allele[$i] ne $allele[$i+1]){ push @newcount, $newcount+1; $newcount = 0; } } print join ("\n", @newcount);
My data is in the array @allele.
HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*03:01 HLA-A*03:01 HLA-A*03:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:01 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03 HLA-A*02:03
In reply to Count non unique groups of elements in an array by Sandy_Bio_Perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |