Link to home
Start Free TrialLog in
Avatar of formadmirer
formadmirerFlag for United States of America

asked on

VFP Char / Num to Nums Only

I found this on a website. It's vb (I think) and it is supposed to take a string consisting of both characters and numbers and return only numbers.

I spent the last hour hacking it up and pretty much destroying it trying to get it to work within vfp9. I'll paste what I've done below the actual code.

Actual code:

Dim Sum As Long = 0
Dim Index As Integer = 1
For Each ch As Char In lcString
	If Char.Isdigit(ch) Then
		Sum += Sum + Integer.Parse(ch) * (Index * 2)
	ElseIf Char.IsLetter(ch) Then
		Select Case ch.ToString.ToUpper
	Case "A"
		Sum += Sum + 10 * (Index * 2)
	Case "B"
		Sum += Sum + 11 * (Index * 2)
	Case "C"
		Sum += Sum + 12 * (Index * 2)
	Case "D"
		Sum += Sum + 13 * (Index * 2)
	Case "E"
		Sum += Sum + 14 * (Index * 2)
	Case "F"
		Sum += Sum + 15 * (Index * 2)
	Case "G"
		Sum += Sum + 16 * (Index * 2)
	Case "H"
		Sum += Sum + 17 * (Index * 2)
	Case "I"
		Sum += Sum + 18 * (Index * 2)
	Case "J"
		Sum += Sum + 19 * (Index * 2)
	Case "K"
		Sum += Sum + 20 * (Index * 2)
	Case "L"
		Sum += Sum + 21 * (Index * 2)
	Case "M"
		Sum += Sum + 22 * (Index * 2)
	Case "N"
		Sum += Sum + 23 * (Index * 2)
	Case "O"
		Sum += Sum + 24 * (Index * 2)
	Case "P"
		Sum += Sum + 25 * (Index * 2)
	Case "Q"
		Sum += Sum + 26 * (Index * 2)
	Case "R"
		Sum += Sum + 27 * (Index * 2)
	Case "S"
		Sum += Sum + 28 * (Index * 2)
	Case "T"
		Sum += Sum + 29 * (Index * 2)
	Case "U"
		Sum += Sum + 30 * (Index * 2)
	Case "V"
		Sum += Sum + 31 * (Index * 2)
	Case "W"
		Sum += Sum + 32 * (Index * 2)
	Case "X"
		Sum += Sum + 33 * (Index * 2)
	Case "Y"
		Sum += Sum + 34 * (Index * 2)
	Case "Z"
		Sum += Sum + 35 * (Index * 2)

		End Select
		End If

		Index += 1
	Next

	Return Sum

Open in new window


me:
FUNCTION char_to_num
LPARAMETERS lcString

Dim Sum As Long
Dim Index As Integer

SUM = 0
INDEX = 1

For Each ch As Char In lcString
	DO CASE
		CASE VARTYPE(ch) = "N"
			Sum = Sum + Integer.Parse(ch) * (Index * 2)
		CASE VARTYPE(ch) = "C"
			ch=UPPER(ch)
	ENDCASE
	
	DO CASE
		Case ch="A"
			Sum = Sum + 10 * (Index * 2)
		Case ch="B"
			Sum = Sum + 11 * (Index * 2)
		Case ch="C"
			Sum = Sum + 12 * (Index * 2)
		Case ch="D"
			Sum = Sum + 13 * (Index * 2)
		Case ch="E"
			Sum = Sum + 14 * (Index * 2)
		Case ch="F"
			Sum = Sum + 15 * (Index * 2)
		Case ch="G"
			Sum = Sum + 16 * (Index * 2)
		Case ch="H"
			Sum = Sum + 17 * (Index * 2)
		Case ch="I"
			Sum = Sum + 18 * (Index * 2)
		Case ch="J"
			Sum = Sum + 19 * (Index * 2)
		Case ch="K"
			Sum = Sum + 20 * (Index * 2)
		Case ch="L"
			Sum = Sum + 21 * (Index * 2)
		Case ch="M"
			Sum = Sum + 22 * (Index * 2)
		Case ch="N"
			Sum = Sum + 23 * (Index * 2)
		Case ch="O"
			Sum = Sum + 24 * (Index * 2)
		Case ch="P"
			Sum = Sum + 25 * (Index * 2)
		Case ch="Q"
			Sum = Sum + 26 * (Index * 2)
		Case ch="R"
			Sum = Sum + 27 * (Index * 2)
		Case ch="S"
			Sum = Sum + 28 * (Index * 2)
		Case ch="T"
			Sum = Sum + 29 * (Index * 2)
		Case ch="U"
			Sum = Sum + 30 * (Index * 2)
		Case ch="V"
			Sum = Sum + 31 * (Index * 2)
		Case ch="W"
			Sum = Sum + 32 * (Index * 2)
		Case ch="X"
			Sum = Sum + 33 * (Index * 2)
		Case ch="Y"
			Sum = Sum + 34 * (Index * 2)
		Case ch="Z"
			Sum = Sum + 35 * (Index * 2)
	ENDCASE
		
	Index = Index + 1
	Next
ENDFOR
	Return Sum

Open in new window


What I get are the following compile errors:
Dim Sum As Long
Error in line 42: Unrecognized command verb.
Dim Index As Integer
Error in line 43: Unrecognized command verb.
ENDFOR
Error in line 113: There is a missing keyword in the FOR...ENDFOR or DO CASE...ENDCASE command structure.

Open in new window



I've yet to get past this point to see if the code actually will do anything.
ASKER CERTIFIED SOLUTION
Avatar of Cyril Joudieh
Cyril Joudieh
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need the Dim in FoxPro.
Avatar of formadmirer

ASKER

CaptainCyril that is fantastic. I can't believe the code is so small.
I'm closing this to award you the points.

I do have another question, but I'll post it separately since you did answer my original question. The new post will ask if this can this be reversed to get the original string back.
Just a small note:

Your first source of code should be suspicious. Code is never of high quality if lines repat in it. This was a useless arrangement of cases. VB also has the ASC() function.

And if the vfp compiler tells you it doesn't knwo a function or command, well, there is the MSDN reference to search for DIM (via google most probably more successful) and find out it's just like LOCAL a variable definition.

Bye, Olaf.