Hi, is it possible to have something like this :
set theWord to "abc" (this is just an example, it can be whatever you want)
set a to 1 as number
set b to 2 as number
set c to 3 as number
...
and then do a math calculation with the letters of theWord set as numbers ?
for example, if theWord is abc, then do 1+2+3
but if I set theWord to “def”, then do 4+5+6
or if I set it to aef, 1+5+6
Thanks
set tLetters to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
set tWord to text returned of (display dialog "Enter the word below" default answer "")
-- No error checking, i.e. could be a number and the sum will be zero.
-- assuming the operation is plus. Comment out "considering case" if capitalization doesn't matter,
-- in which case you can leave them out of tLetters too. Without considering... AppleScript won't
-- distinguish between upper and lower case.
set tSum to 0
considering case
repeat with aLetter in tWord
set tSum to tSum + (offset of aLetter in tLetters) -- values between 1 and 52
end repeat
end considering
tSum
Hi,
try this
set letters to "abcdefghijklmnopqrstuvwxyz"
set sample to "Hello"
set total to 0
repeat with i in characters in sample
set total to total + (offset of i in letters)
end repeat
total --> 52
Late to the party but here’s my take.
set alphaList to "abcdefghijklmnopqrstuvwxyz"
set finalSum to 0
repeat with char in (text items of (text returned of (display dialog "Gimme something to count: " & return & "(Punctuation and numbers are ignored) " default answer "")))
set finalSum to finalSum + (the offset of char in alphaList)
end repeat
display dialog "That adds up to " & finalSum buttons {"Quit"} default button 1 giving up after 2
Have fun,
Jim
Thanks both of you for your quick replies !
One more thing, is it also possible to do stuff like 2 * 3 + (2 ^ 2) ? or just always one of these at the time : +, -, *, /, ^ and <square root, don’t know the symbol> ?
i.e. set theWord to “script”
then do s * c + r * ( 2 * i + p) - t
Or is it just always the same calculations that needed to be used ?
If you wanted to assign numbers to letters and then sum them, this does it:
set tLetters to "abcdefghijklmnopqrstuvwxyz"
set tValues to {26, 3, 1, 22, 16, 10, 23, 18, 19, 5, 2, 11, 14, 20, 15, 17, 24, 7, 13, 6, 9, 21, 4, 12, 8, 25} -- none repeated; a random list of nums from 1 to 26
set tWord to text returned of (display dialog "Enter the word below" default answer "")
-- assuming the operation is plus. Case ignored.
set tSum to 0
repeat with aLetter in tWord
set tSum to tSum + (item (offset of aLetter in tLetters) of tValues)
end repeat
tSum --> for JamesGreen, gives 143
James;
It’s possible, but the operators to be used (and where) have to be specified somehow. You’d need a rule, and the input word would have to be sized to accommodate the operator rule.
Ok thanks, I’ll look into it but thanks for giving me a start, I couldn’t find that on my own for sure. Thanks
Applescript is fully capable of performing algebra such as (l * (r - 1)) + c. Just keep in mind that some functions, e.g. a square or cube root, have to be done using a work around. E.g. the square root of 4 in AppleScript would be:
4 ^ 0.5
As the root of any number is equal to the same number taken to the inverse of the root, e.g. x√y = y ^ (1/x)
Oh, thanks but I didn’t meant that, I meant like :
set a to 44
set b to 73
set c to 22
set d to 500
…
and then if a word is typed, for example, “acd”, then when a “a” is typed, then multiply it by c if it’s in the word etc.
And btw, thanks for the square root thing, I completely forgot x ^ 0.5…
And what about getting the numbers back to text ? is that possible?