Page 1 of 2
How to use an array?
Posted: Fri Mar 25, 2022 8:47 am
by AlessioForconi
Salutations,
I would like to replace the letters of the alphabet with numbers.
A = 1 b = 2 c = 3 ...
I thought I was using an array instead of a switch, 26 possibilities are too many, to find out what the letter is and extract its number.
However, I cannot implement it correctly. Could I have a simple example of how to do it?
Code: Select all
Function convertprimary mywords
Put MyWords Into Pippo
Repeat for Each Char Item in Pippo
Add 1 to i
Put Char I of Pippo Into letters
---> Here should be the array for replacement <---
Answer letters
End Repeat
End ConvertPrimary
thank you so much
Re: How to use an array?
Posted: Fri Mar 25, 2022 11:00 am
by AndyP
No need to use arrays, try this in a button
Code: Select all
on mouseUp
put "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26" into tNumbers
put "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" into tLetters
set the itemDel to comma
repeat with c = 1 to the number of items in tNumbers
put item c of tLetters into item c of tNumbers
end repeat
answer tNumbers
--if you want to get rid of the commas
replace "," with space in tNumbers
answer tNumbers
end mouseUp
Re: How to use an array?
Posted: Fri Mar 25, 2022 11:10 am
by Klaus
Buongiorno Alessio,
really not sure what you are looking for?
You want to have an array like:
a[1]
b[2]
etc...
and use this to replace the characters in a give text?
If yes, then do like this:
Code: Select all
Function convertprimary mywords
## Create the array:
## We will use repat for EACH so we need to manage a counter
put 1 into tCounter
put empty into tCharArray
repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz"
put tCounter into tCharArray[tChar]
add 1 to tCounter
end repeat
## Now replace each char with its number from the array
## We create a new variable which contains the replaced character
put empty into myNewWords
Repeat for Each Char tChar2 in mywords
put tCharArray[tChar2] after myNewWords
End Repeat
return myNewWords
End ConvertPrimary
May need some refinement, but you get the basic idea.
If this is not what you mean, please explain.
Best
Klaus
Re: How to use an array?
Posted: Fri Mar 25, 2022 1:56 pm
by andresdt
AlessioForconi wrote: ↑Fri Mar 25, 2022 8:47 am
Salutations,
I would like to replace the letters of the alphabet with numbers.
A = 1 b = 2 c = 3 ...
I thought I was using an array instead of a switch, 26 possibilities are too many, to find out what the letter is and extract its number.
Besides the solutions given here, I can think of two others.
1
Code: Select all
on mouseUp
answer convertprimary("E")
end mouseUp
function convertprimary pLetter
return itemOffSet(toUpper(pLetter),"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
end convertprimary
2
Code: Select all
on mouseUp
answer convertprimary("E")
end mouseUp
local sLetterArray
function convertprimary pLetter
if sLetterArray is not an array then
local c = 0
repeat for each item i in "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
add 1 to c
put c into sLetterArray[i]
end repeat
end if
return sLetterArray[ pLetter ]
end convertprimary
The first version besides being more compact is faster
---Edit---
What you have to do is call one of these two algorithms inside a loop.
Code: Select all
function LetterToNum pWord
local tNexText
repeat for each char tChar in pWord
put convertprimary(tChar) after tNexText
end repeat
return tNexText
end LetterToNum
Re: How to use an array?
Posted: Fri Mar 25, 2022 1:58 pm
by dunbarx
Lots of ways to do what you want, but I also am wondering why you would want to.
With, say, Klaus' handler, you will end up with both one and two digit replacements for a body of text. You will need a routine to parse that numeric string. What will you do with spaces, actual numbers in your text or other characters besides alpha ones? How will you use the numeric string obtained?
At least think about this, with at least each derived char separated by a space:
Code: Select all
on mouseUp
answer translateCharsToNumbers("my dog has fleas")
end mouseUp
function translateCharsToNumbers var
repeat for each char tChar in var
put charTonum(tChar) - 96 & space after temp
end repeat
return temp
end translatecharsToNumbers
This is only valid for lower case letters, another issue perhaps for you to think about.
Craig
Re: How to use an array?
Posted: Fri Mar 25, 2022 3:15 pm
by stam
Agree with all above - i was thinking that for this simple purpose a list (rather than array) would do - but then you don't even need that...
As Craig suggests you can just extrapolate from the ASCII code.
Personally i would use toUpper(<letter>) and then subtract 64 from the ascii code, so that A or a = 1, B or b = 2, etc. But basically what Craig says!
For the OP's benefit if wanting to learning more about arrays (above and beyond the documentation from LC), this may be helpful:
http://livecodeblog.blogspot.com/2015/0 ... rrays.html
Stam
Re: How to use an array?
Posted: Fri Mar 25, 2022 3:26 pm
by dunbarx
You can see that in my quick handler a dash ("-") is generated where a space was encountered in the source. Of course that can be detected and either exploited or ignored. Similarly, upper case source letters can be detected and similarly handled.
But it occurs to me again, that though the mechanics of building what you want are simple, I think that your purpose in all this may suggest a completely different approach.
So can you say what you are up to?
Craig
Re: How to use an array?
Posted: Fri Mar 25, 2022 3:28 pm
by dunbarx
Stam.
i would use toUpper(<letter>) and then subtract 64 from the ascii code, so that A or a = 1, B or b = 2, etc.
A cute gadget. But I still wonder what the intent behind all this is. I bet the best solution will address that intent more than the coding mechanics.
Craig
Re: How to use an array?
Posted: Fri Mar 25, 2022 3:41 pm
by dunbarx
Stam's enhancement, and losing spaces and other fluff in the source text, is worth publishing here:
Code: Select all
on mouseUp
answer translateCharsToNumbers("My dOg2 & ? HaS flEAs")
end mouseUp
function translateCharsToNumbers var
put "abcdefghijklmnopqrstuvwxyz" into pool
repeat for each char tChar in var
if tChar is in pool then put charTonum(toUpper(tChar)) - 64 & space after temp
end repeat
return temp
end translateCharsToNumbers
Craig
Re: How to use an array?
Posted: Fri Mar 25, 2022 3:57 pm
by Klaus
Hi Craig,
numtochar() and chartonum() are deprecated since version 7 and may throw an error in later versions!
Get used to use bytetonum() and numtobyte() instead.
Best
Klaus
Re: How to use an array?
Posted: Fri Mar 25, 2022 4:06 pm
by dunbarx
Klaus.
I know, and you are correct. But I am going wait until my code breaks to change.
To everyone else, change now.
Craig
Re: How to use an array?
Posted: Fri Mar 25, 2022 4:19 pm
by Klaus
Lazybone!

Re: How to use an array?
Posted: Fri Mar 25, 2022 4:35 pm
by stam
actually numToByte() and byteToNum() is for binary data i think!
The correct versions of this would be
numToNativeChar() and
nativeCharToNum() 
Re: How to use an array?
Posted: Fri Mar 25, 2022 4:59 pm
by Klaus
Ouch, you are correct, of course.

Re: How to use an array?
Posted: Fri Mar 25, 2022 5:05 pm
by dunbarx
Ouch?
I feel no pain.
Craig