Depending on how precise you want to be about equality, you might need to do a fancy floating-point compare:

my $EPSILON = 0.01; sub approx_equal ( $ $ ) { my ( $f1, $f2 ) = @_; return abs( $f1 - $f2 ) < $EPSILON; }

The most straigntforward filter-style code I could come up with:

while ( <> ) { my @in_range = grep { /^[\d.+-]+$/ && 296.1 <= $_ && $_ <= 314.0 } split /,\s*/; # skip records with only 305.1 (room temp kelvin?) in range next if @in_range == 1 && approx_equal( $in_range[0], 305.1 ); print; }

If this is too slow, it might be faster to bail out as soon as we know we can:

LINE: while ( my $line = <> ) { my $n_305_1 = 0; my $n_in_range = 0; foreach my $chunk ( split /,\s*/, $line ) { # skip non-numeric next unless $chunk =~ /^[\d.+-]+$/; # skip values outside the range next unless 296.1 <= $chunk && $chunk <= 314.0; # only allowed one value in the given range... if ( ++ $n_in_range >= 2 ) { print $line; next LINE; } # and we can only ignore one 305.1 if ( approx_equal( $chunk, 305.1 ) && ++$n_305_1 > 1 ) { print $line; next LINE; } } print $line unless $n_in_range == 1 && $n_305_1 == 1; if ( $n_in_range == 0) { warn "ill-formed line $.: no values in range"; } }

In reply to Re: Seeking Algorithm by tkil
in thread Seeking Algorithm by WhiteBird

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.