The following code seems to do what you are after, although a module solution may be more appropriate.

use warnings; use strict; use constant kMaxDelta => 0.15; my @rawData = (<DATA>); chomp @rawData; @rawData = sort {$a <=> $b} @rawData; my @groups; my @currGroup = (shift @rawData); for (@rawData, undef) { if (defined $_ and abs ($_ - sum (@currGroup, $_) / (@currGroup + +1)) <= kMaxDelta) { # Ok to add to current group push @currGroup, $_; } else { # time for a new group push @groups, [@currGroup]; @currGroup = ($_); } } my $count = 1; for my $group (@groups) { if (@$group == 1) { print "Outlier: $group->[0]\n\n"; } else { my $avg = sum (@$group) / @$group; my @outliers; my @ok; abs ($avg - $_) > kMaxDelta ? push @outliers, $_ : push @ok, $ +_ for @$group; printf "group$count: Average: %.2f\n", $avg; printf "$_ diff=%.2f\n", abs ($avg - $_) for (@ok); print "\n"; ++$count; printf "Outlier: $_ since dif=%.2f>X\n\n", abs ($avg - $_) for + @outliers; } } sub sum { my $sum = shift; $sum += $_ for @_; return $sum; } __DATA__ 100.20 100.23 100.35 122.43 122.55 122.67 122.75 145.88 145.97 146.01 146.10

Prints:

group1: Average: 100.26 100.20 diff=0.06 100.23 diff=0.03 100.35 diff=0.09 group2: Average: 122.60 122.55 diff=0.05 122.67 diff=0.07 122.75 diff=0.15 Outlier: 122.43 since dif=0.17>X group3: Average: 145.99 145.88 diff=0.11 145.97 diff=0.02 146.01 diff=0.02 146.10 diff=0.11

Note that the results you gave are inconsistent with the data you gave. I have altered 122.45 to 122.43 to give the same outlier result but that has altered the deltas for the second group.


DWIM is Perl's answer to Gödel

In reply to Re: Grouping numbers by GrandFather
in thread Grouping numbers by tamaguchi

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.