Page 1 of 1

minIntExceptZero(a,b) - a Min() procedure which ignores Zero

Posted: Sat Mar 01, 2014 1:09 am
by EfDschoma
In a complex string manipulation, I try to cut off trailing code ( Comments ( ; xyz) and concatenated code lines ( a = 10 : b = 11 )
so i fetch the position of ; and of : an then I compute the min() value, but i have to ignore the zero values ( e.g. no semicolon found )

Therefore, I designed this code

Code: Select all

Procedure minIntExceptZero( a.l, b.l )
  If     a = 0 : ProcedureReturn b      ; if a = 0 return b, anyway if b = 0 or b > 0   
  ElseIf b = 0 : ProcedureReturn a      ; a = 0 return b, anyway if b = 0 or b > 0
  ElseIf a < b                          ; a > 0 und a < b. gib a zurück
    ProcedureReturn a
  Else   
    ProcedureReturn b
  EndIf
EndProcedure

test$        = "a = 10 : b = 11 ; i will extract only: a=10"
posColon     = FindString( test$, ":"  )
posSemiColon = FindString( test$, ";" )
posCut       = minIntExceptZero( posColon, posSemiColon )
Debug Trim( Left( test$, posCut - 1 ) )
Try it with

Code: Select all

Procedure minIntExceptZero( a.l, b.l )
  If     a = 0 : ProcedureReturn b      ; if a = 0 return b, anyway if b = 0 or b > 0    
  ElseIf b = 0 : ProcedureReturn a      ; a = 0 return b, anyway if b = 0 or b > 0
  ElseIf a < b                          ; a > 0 und a < b. gib a zurück
    ProcedureReturn a
  Else    
    ProcedureReturn b
  EndIf
EndProcedure

Debug minIntExceptZero ( 1, 2 )  ; returns 1
Debug minIntExceptZero ( 0, 2 )  ; returns 2
Debug minIntExceptZero ( 3, 1 )  ; returns 1
Debug minIntExceptZero ( 3, 0 )  ; returns 3
Does someone know a better and/or faster code

Re: minIntExceptZero(a,b) - a Min() procedure which ignores

Posted: Sat Mar 01, 2014 1:52 am
by IdeasVacuum
Extract the number values first and then select as required. Extraction is efficient using StringField()

Code: Select all

Define iA.i, iB.i, sA.s, sB.s
Define sTest.s = "a = 10 : b = 11 ; i will extract only: a=10"

         sTest = StringField(sTest, 1, ";")

            sA = StringField(sTest, 1, ":")
            iA = Val(StringField(sA, 2, "="))

            sB = StringField(sTest, 2, ":")
            iB = Val(StringField(sB, 2, "="))

Debug "iA-->" + Str(iA) + "<--"
Debug "iB-->" + Str(iB) + "<--"

Re: minIntExceptZero(a,b) - a Min() procedure which ignores

Posted: Sat Mar 01, 2014 11:23 am
by infratec
Btw.

I think it's more a coding question since a tip or trick.

Bernd

Re: minIntExceptZero(a,b) - a Min() procedure which ignores

Posted: Wed Mar 05, 2014 11:34 am
by em_uk
He did say
"Does someone know a better and/or faster code"
:)