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

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.