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

Just starting out? Need help? Post your questions and find answers here.
User avatar
EfDschoma
User
User
Posts: 17
Joined: Fri Feb 21, 2014 11:46 am

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

Post 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
I may not know much, but I try a lot
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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) + "<--"
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

Btw.

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

Bernd
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

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

Post by em_uk »

He did say
"Does someone know a better and/or faster code"
:)
----

R Tape loading error, 0:1
Post Reply