Sandy_Bio_Perl has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Count non unique groups of elements in an array
by choroba (Cardinal) on Aug 15, 2016 at 00:52 UTC
    Just repeat the push line after the loop ends:
    push @newcount, $newcount + 1;

    BTW, I'd also recommend to rename @newcount to @newcounts to avoid confusion with $newcount .

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you. Great!

Re: Count non unique groups of elements in an array
by Anonymous Monk on Aug 15, 2016 at 02:57 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1169780 use strict; use warnings; my @allele = <DATA>; my @newcounts; $_ = join '', @allele; push @newcounts, $& =~ tr/\n// while /^(.*\n)\1*/gm; print for @newcounts; __DATA__ 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

      Thank you. Very neat solution