Counting Letters

S

Sharkbyte

Is there a way to 'count' the difference between 2 letters? Such as being
able to determine that if I begin with "M" then "K" and "O" are differences
of 2.

I know I could do it by associating each letter to a number, but I wanted to
save the time of doing the lookups, if at all possible.

TIA

Sharkbyte
 
D

Dirk Goldgar

Sharkbyte said:
Is there a way to 'count' the difference between 2 letters? Such as being
able to determine that if I begin with "M" then "K" and "O" are
differences
of 2.

I know I could do it by associating each letter to a number, but I wanted
to
save the time of doing the lookups, if at all possible.


?Asc("O") - Asc("M")
2

Note that this is case-sensitive:

?Asc("o") - Asc("m")
2
?Asc("O") - Asc("m")
-30

And it relies on the standard ASCII character mapping.
 
J

JvC

Look at the Asc() function.
Asc("M") = 77
Asc("K") = 75

John

Sharkbyte wrote on 9/11/2008 :
 
M

Maverick

Sorry, no easy route on this one. There is nothing in code or function (which
I know of) that sets values for letters. You will need to define the value of
each letter to do the calculation.
--------------------------------------------

HTH

Don''t forget to rate the post if it was helpful!

Please reply to newsgroup only, so that others may benefit as well.
 
S

Sharkbyte

I should be able to use the UCase function to maintain consistency with
Ascii, correct?
 
D

Dirk Goldgar

Sharkbyte said:
I should be able to use the UCase function to maintain consistency with
Ascii, correct?

Sure. As in:

Function LetterDistance(FirstLetter As String, SecondLetter As String) _
As Integer

If FirstLetter = SecondLetter Then
LetterDistance = 0
ElseIf Len(FirstLetter) = 0 Then
LetterDistance = 32767 ' arbitrary
ElseIf Len(SecondLetter) = 0 Then
LetterDistance = -32767 ' equally arbitrary
Else
LetterDistance = _
Asc(UCase$(SecondLetter)) - Asc(UCase$(FirstLetter))
End If

End Function
 
J

John W. Vinson

Is there a way to 'count' the difference between 2 letters? Such as being
able to determine that if I begin with "M" then "K" and "O" are differences
of 2.

I know I could do it by associating each letter to a number, but I wanted to
save the time of doing the lookups, if at all possible.

TIA

Sharkbyte

You can use the Asc() function to return the ASCII value of the character.
Note that "a" and "A" have different values - 97 and 65 respectively. You may
want to use Asc(Ucase([fieldname])).
 
L

Larry Linson

Maverick said:
Sorry, no easy route on this one. There is nothing in code or function
(which
I know of) that sets values for letters. You will need to define the value
of
each letter to do the calculation.
--------------------------------------------

HTH

Don''t forget to rate the post if it was helpful!

Please reply to newsgroup only, so that others may benefit as well.

Good thing most people don't read through the online interface and, thus,
can't "rate" this response, eh? If you'll read the responses, you'll find
that text characters are already assigned numbers... they are, in fact, just
a display interpretation of numbers.
 
Top