Author |
Message |
Guest
|
Posted: Tue Aug 22, 2006 5:52 pm Post subject: Assigning Letter Values |
|
|
How would I automate this code in a for loop preferably?
code: |
letter (1) := 'a'
letter (2) := 'b'
letter (3) := 'c'
letter (4) := 'd'
letter (5) := 'e'
letter (6) := 'f'
letter (7) := 'g'
letter (8) := 'h'
letter (9) := 'i'
letter (10) := 'j'
letter (11) := 'k'
letter (12) := 'l'
letter (13) := 'm'
letter (14) := 'n'
letter (15) := 'o'
letter (16) := 'p'
letter (17) := 'q'
letter (18) := 'r'
letter (19) := 's'
letter (20) := 't'
letter (21) := 'u'
letter (22) := 'v'
letter (23) := 'w'
letter (24) := 'x'
letter (25) := 'y'
letter (26) := 'z'
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Tue Aug 22, 2006 6:14 pm Post subject: (No subject) |
|
|
You should really learn about "ord" and "chr", there amazing
code: |
var letter: array 1 .. 26 of string
for i: 1 .. upper (letter)
letter(i) := chr(64+i)
put letter(i) ..
end for
|
|
|
|
|
|
|
pj_ladd12
|
Posted: Tue Aug 22, 2006 8:27 pm Post subject: (No subject) |
|
|
why did you 64+i? why 64? |
|
|
|
|
|
TokenHerbz
|
Posted: Tue Aug 22, 2006 8:42 pm Post subject: (No subject) |
|
|
to get the capital alphabets, go ahead and take out "64" and see what happends. |
|
|
|
|
|
Cervantes
|
Posted: Tue Aug 22, 2006 8:45 pm Post subject: (No subject) |
|
|
"A" is given the ASCII value 65. "B" is 66, and so on. So since we're starting with an index of 1, we need to add 64 to our index to get us to the range of the capital letters.
To get lower case letters like the author wanted, add a higher value. "a" is ASCII value 97. |
|
|
|
|
|
Clayton
|
Posted: Wed Aug 23, 2006 1:55 am Post subject: (No subject) |
|
|
another way to do it (not as good, but works fine) is to have a string variable with all of the letters of the alphabet (lower and upper case) and simply having a for loop giving the value of a letter to an element in your array, for example:
Turing: |
var alphabet : string := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var my_letters : array 1 .. length(alphabet ) of string
for x: 1.. upper(my_letters )
my_letters (x ):=alphabet (x )
end for
|
|
|
|
|
|
|
Cervantes
|
Posted: Wed Aug 23, 2006 7:36 am Post subject: (No subject) |
|
|
Since a string is basically an array of characters, you've got two arrays of characters there. Not so efficient. You could probably work with your string ("abcd...XYZ") just as easily as you work with the array that was originally posted. |
|
|
|
|
|
Wolf_Destiny
|
Posted: Wed Aug 23, 2006 8:05 am Post subject: (No subject) |
|
|
Here you go:
code: | var letter : array 1..26 of string (1)
for something: 'a'..'z'
letter (ord (something)-ord ('a') + 1) := something
end for |
Four lines of quick for loop goodness.
To make capitals instead, all you have to do it change 'a' to 'A' and 'z' to 'Z'
It also works with numbers... or any characters really
Hope that helps |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Wed Aug 23, 2006 2:38 pm Post subject: (No subject) |
|
|
my way is best, as it is far easier to get any characters needed, and just as easy to change them. |
|
|
|
|
|
Cervantes
|
Posted: Wed Aug 23, 2006 3:10 pm Post subject: (No subject) |
|
|
They're basically the exact same thing. Let's not squabble over that. The question has been answered.
No more flogging a dead horse. |
|
|
|
|
|
|