procmail
[Top] [All Lists]

Re: Variable greater/less than testing

2009-08-25 02:15:55
At 22:21 2009-08-24 +0100, Stephen Allen wrote:
At the moment, if 'foo' isn't set or is set, but null, it gets assigned $maxValue. Now let's say foo is set to "foobar", I would need to recognise it's not a number (perhaps -eq 0), and then like before, assign it $maxValue.

I would probably want to make sure the value assigned to foo is within certain min/max limits which is why I need to work out a simple way of implementing less/greater than.

# this will check to see that foo is only a numeric
# preceed the condition ling with the inversion flag (!) if you want to
# trip on non-numeric.  Note that an EMPTY string will evaluate as
# non-numeric - you can test for empty if you want to handle it a certain
# way.
# The regexp may seem a bit obtuse, but specifically, you want to avoid
# allowing something like "-." to be parsed as valid, but you do want
# -0 -0. -0.0 -.0 to be.  Modify as appropriate.  You might want to allow
# for leading and trailing whitespace as well, but that's your call.
:0
* ! foo ?? ^^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)^^
action_not_all_numeric

# will evaluate true if $foo is greater than 1000.
# basically, we start with a score of negative 1000, and then add foo - if the
# result is positive, it is greater than 1000.
:0
* -1000^0
* $ $foo^0
some_action

# will evaluate true if $foo is less than 1000.
# start with a score of 1000, and add a negated foo.  If foo is 999, then
# the result (1000 + (-)999) is positive.  if it's 1000, the result is 0,
# 1001, -1.  If foo is NEGATIVE, then it will add:  (1000 + (-)-1), which
# results in 1001, and thus continues to evaluate true (it's less than 1000)
:0
* 1000^0
* $ -$foo^0
some_action


I did't test any of these before posting, so there might be errors. If so, sorry.

---
 Sean B. Straw / Professional Software Engineering

 Procmail disclaimer: <http://www.professional.org/procmail/disclaimer.html>
 Please DO NOT carbon me on list replies.  I'll get my copy from the list.

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)de
http://mailman.rwth-aachen.de/mailman/listinfo/procmail

<Prev in Thread] Current Thread [Next in Thread>