Page 1 of 1

Strange behaviour at procedure's parameter - IT WASN'T

Posted: Mon Nov 16, 2015 6:48 pm
by thanos
Hello.
I tried to translate the old SoudEx() c function which is used by Clipper for Dos.
The forth call always sends an empty string in the procedure.
If you the procedure 3 times, everything is okay.
If you call the procedure more than 3 times the 4th call always send to the proc an empty string.

Code: Select all

;**
;   SoundexC()
;   Convert a string To a soundex code (C-callable).
;
;	"Soundex" algorithm is standard Odell/Russell (1918):
;
;	Produce a code of the form "letter, digit, digit, digit"
;	using these rules:
;
;	1)	Retain the first letter unchanged.
;
;	2)	For each succeeding letter, produce a result based
;		on the following table:
;
;		letter							result
;
;		B, F, P, V						digit 1
;		C, G, J, K, Q, S, X, Z			digit 2
;		D, T							digit 3
;		L								digit 4
;		M, N							digit 5
;		R								digit 6
;		A, E, H, I, O, U, W, Y			(nothing)
;
;
;	3)	If two Or more adjacent letters produce the same
;		result IN Step 2, ignore all but the first of the
;		adjacent letters.
;
;	4)  Repeat steps 2 And 3 Until three digits have been
;		produced Or Until the source is exhausted.
;
;	5)	If less than three digits were produced, right-fill
;		With zeros.
;
;
;	Notes:
;
;	Non-alpha characters are ignored entirely; letters which
;	are separated only by non-alpha characters are considered
;	adjacent.  If the source contains no alpha characters, a
;	value of "0000" is returned.
;
;	Case is Not significant.
;
;	Letters which produce (nothing) IN Step 2 are still
;	significant With respect To Step 3.  That is, two letters
;	which produce the same digit are Not considered adjacent
;	If they are separated by a letter that produces (nothing).
;	This is IN accordance With the original algorithm.
;
;	This C-callable function returns a pointer To a Static
;	buffer.  The buffer is overwritten by successive calls.
;/

EnableExplicit

#SOUNDEX_LENGTH = 4

Macro IsAlpha(c)
  ((c) >= "a" And (c) <= "z") Or ((c) >= "A" And (c) <= "Z")
EndMacro


Procedure Step2(Char.s)
  Protected Result

  Select Char
    Case "B", "F", "P", "V"
      Result = 1
  
    Case "C", "G", "J", "K", "Q", "S", "X", "Z"
      Result = 2
  
    Case "D", "T"
      Result = 3
  
    Case "L"
      Result = 4
  
    Case "M", "N":
      Result = 5
  
    Case "R"
      Result = 6
  
    Case "A", "E", "H", "I", "O", "U", "W", "Y"
      ;~ Return (NIL);
      Result = #Null
  
    Default 
      ;~ /* bad param -- Return something obviously wrong */
      Result =  9
  EndSelect
  
  ProcedureReturn Result
EndProcedure


Procedure.s SoundexC(Source.s)
  Protected i
  Protected Char.s{1}
  Protected Result
  Protected Previous
  Protected Length = Len(Source)
  Protected Code.s

  For i = 1 To Length
    Char = UCase(Mid(Source, i, 1))
    If (IsAlpha(Char))
      Result = Step2(Char)
      If (i = 1)
        Code + Char
      ElseIf ((Result <> #Null)  And (Result <> Previous))
        Code + Str(Result)
      EndIf
      Previous = Result
    EndIf
  Next  
  Code = LSet(Code, #SOUNDEX_LENGTH, "0")

  ProcedureReturn Code
EndProcedure


Procedure.s SoundEx(sText.s)
  Protected sCode.s

  If (Len(sText) >= 1) And IsAlpha(Left(sText, 1))
    sCode = SoundexC(sText)
  Else
    sCode = "0000"
  EndIf

  ProcedureReturn sCode
EndProcedure


Debug SoundEx("kakaoudas")           ; It must be K232
Debug SoundEx("Thanos Douros")       ; It must be T523
Debug SoundEx("DUROS ATHANASIOS")    ; It must be D623
Debug SoundEx("ntouros athanasios")  ; It must be N362 BUT RETURNS 0000 which is an empty string
Debug SoundEx("kakao")               ; It must be K200
Debug SoundEx("nounou")              ; It must be N500
Debug SoundEx("elephant")            ; It must be E415



Any idea?
Regards.

Thanos

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 6:55 pm
by Kiffi
no problem here:
Debugger wrote:K232
T523
D623
N362
K200
N500
E415
Greetings ... Peter

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 7:02 pm
by thanos
Kiffi wrote:no problem here:
Debugger wrote:K232
T523
D623
N362
K200
N500
E415
Greetings ... Peter
Thanks Peter for the quick response.
Which version of compiler are you using?

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 7:04 pm
by thanos
I tried the code in PB 4.61x86, 5.21x86, 5.31x86 and it run smoothly.
It also run correct with PB 5.40x64
I have this problem only with 5.40x86 compiler.

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 8:08 pm
by Kiffi
thanos wrote:Which version of compiler are you using?
Windows 10 / PB 5.40 (x86) (with and without Unicode)

Grüße ... Peter

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 8:35 pm
by netmaestro
PureBasic 5.40 x86 MacOS El Capitan

...Works perfectly. Results are as listed, fully acceptapple.

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 8:48 pm
by kenmo
Works OK for me, on Win 7 / PB 5.40 / x86.

Code: Select all

Macro IsAlpha(c)
  ((c) >= "a" And (c) <= "z") Or ((c) >= "A" And (c) <= "Z")
EndMacro
Are the >= and <= operators supported for strings? I honestly don't know. I never use them that way.

Re: Strange behaviour at procedure's parameter

Posted: Mon Nov 16, 2015 8:50 pm
by thanos
:shock:
After rebooting the pc the program runs smoothly in 5.40x86
Thank you all for the replies but do you have an idea what could be wrong?

Re: Strange behaviour at procedure's parameter

Posted: Tue Nov 17, 2015 8:22 am
by thanos
kenmo wrote:Works OK for me, on Win 7 / PB 5.40 / x86.

Code: Select all

Macro IsAlpha(c)
  ((c) >= "a" And (c) <= "z") Or ((c) >= "A" And (c) <= "Z")
EndMacro
Are the >= and <= operators supported for strings? I honestly don't know. I never use them that way.
I tried it and it is working :D

Re: Strange behaviour at procedure's parameter

Posted: Tue Nov 17, 2015 8:37 am
by Dude
kenmo wrote:Are the >= and <= operators supported for strings?
Of course! Since the Basic language was invented.