[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VFP Char / Num to Nums Only

Posted on 2012-09-10
4
Medium Priority
?
571 Views
Last Modified: 2012-09-10
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.
0
Comment
Question by:formadmirer
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
CaptainCyril earned 2000 total points
ID: 38383193
Sum = 0
Index = 1
FOR i = 1 TO LEN(lcString)
	ch = SUBSTR(lcString, i, 1)
	IF ISDIGIT(ch)
		Sum = Sum + VAL(ch)*(Index*2)
	ELSE
		IF ISALPHA(ch)
			SUM = SUM + (ASC(ch)-55)*(Index*2)
		ENDIF
	ENDIF
	Index = Index + 1
ENDFOR
Return Sum

Open in new window

0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 38383201
You don't need the Dim in FoxPro.
0
 

Author Closing Comment

by:formadmirer
ID: 38383373
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.
0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 38383447
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question