Page 1 of 1

Little trick: macro "between"

Posted: Sat May 05, 2012 9:12 am
by graves
Hi,
For long time, on PB I missed the SQL command "between", but it's very simple using a macro:

Code: Select all

macro between(btwfld, btwfrm, btwto)
   btwfld >= btwfrm AND btwfld <= btwto
endmacro

if between(fielddata, 100, 1000)
   ... code
endif
(you can use it without paying royalties)

Re: Little trick: macro "between"

Posted: Sun May 06, 2012 6:48 am
by Guimauve
Hello everyone,

A more safe approach to this is to add parentheses around each parameters (it's a workaround to solve macro bad substitution when the macro parameter look like Var + 5) :

Code: Select all

Macro IsBetween(Value, Lower, Upper)
	
	((Value) >= (Lower) And (Value) <= (Upper))
	
EndMacro

Macro IsNotBetween(Value, Lower, Upper)
	
	((Value) < (Lower) Or (Value) > (Upper))
	
EndMacro


For Index = 5 To 15
  
  If IsBetween(Index, 8, 12)
    Debug "IsBetween 8 and 12 : " + Str(Index)
  EndIf
  
Next

For Index = 5 To 15

  If IsNotBetween(Index, 8, 12)
    Debug "IsNotBetween 8 and 12 : " + Str(Index)
  EndIf
  
Next
Best regards
Guimauve

Re: Little trick: macro "between"

Posted: Sun May 06, 2012 7:17 am
by Little John
Guimauve wrote:

Code: Select all

Macro IsNotBetween(Value, Lower, Upper)
  
  ((Value) <= (Lower) And (Value) >= (Upper))
  
EndMacro
With or without parentheses ... your Macro IsNotBetween() will not work correctly. It must use Or instead of And. Additionally, when IsBetween() uses >= and <=, IsNotBetween() shouldn't use that, but > and < instead for the sake of consistent Boolean logic:

Code: Select all

Macro IsNotBetween(Value, Lower, Upper)
  ((Value) < (Lower) Or (Value) > (Upper))
EndMacro
Regards, Little John

Re: Little trick: macro "between"

Posted: Sun May 06, 2012 2:52 pm
by Tenaja
Yes, "technically," you should call it IsFrom() if you want to include the extremes.