Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Match Numeric Range

by pme (Prior)
on Aug 18, 2015 at 15:51 UTC ( #1139037=note: print w/replies, xml ) Need Help??


in reply to Match Numeric Range

Hi PilotinControl,

You may apply hystersis. The state will be set to 1 if the value goes above three times the upper limit or it will be set to 0 if the value goes below lower limit three times.

#!/usr/bin/perl use strict; use warnings; my $state = 0; # 0 - light off, 1 - light on my $uplim = 102; # upper limit my $lolim = 98; # lower limit my $n = 3; # my $counter = 0; # Set initial state # This part may need more work in order to avoid eventual starting gli +tch. my $value = <DATA>; chomp $value; if ($value > (($uplim + $lolim) / 2.0)) { $state = 1; } else { $state = 0; } # work continuously while ($value = <DATA>) { chomp $value; # Adjust counter if ($value > $uplim) { if ($counter < 0) { $counter = 0; } else { $counter++; } } elsif ($value < $lolim) { if ($counter > 0) { $counter = 0; } else { $counter--; } } else { $counter = 0; } # Set state if ($counter > $n) { $state = 1; } elsif ($counter < -$n) { $state = 0; } # Print my ($below, $between, $above); $below = $between = $above = '---'; if ($value > $uplim) { $above = $value; } elsif ($value < $lolim) { $below = $value; } else { $between = $value; } printf "state: %d cnt: %+3d %3s<%3d>%3s<%3d>%3s\n", $state, $counter +, $below, $lolim, $between, $uplim, $above; } __DATA__ 100 101 102 103 104 105 106 105 104 103 102 101 100 99 98 97 96 95 94 101 102 103 103 102 103 103 104 104

Replies are listed 'Best First'.
Re^2: Match Numeric Range
by PilotinControl (Pilgrim) on Aug 19, 2015 at 14:11 UTC

    Hello PME!

    I will most certainly take a look at this as this code may have a good use on some other functions I am trying to achieve thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1139037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2019-11-19 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (96 votes). Check out past polls.

    Notices?