Page 1 of 1
Split a Fraction into Words?
Posted: Fri Oct 09, 2020 2:14 pm
by cmhjon
Hi all,
Suppose I have "9/32" in a text field (the field could contain one of MANY other mathematical fractions). Via script, I want to separate it into parts. This is the script I have:
Code: Select all
on mouseUp
set the itemDelimiter to "/"
answer word 1 of field "Test"
end mouseUp
It still answers "9/32" so how can I split the fraction into two words/parts/chunks (not sure what the proper word is here) so that "9" is one word and "32" is another word? My intention is to use the textShift property to make the fraction look more like a (diagonal) fraction:
If there is a better way to do this, I am all ears.
Thank you,
Jon

Re: Split a Fraction into Words?
Posted: Fri Oct 09, 2020 3:11 pm
by Opaquer
Hi Jon
I'm not sure if there's a better way, and I'm not at my computer so can't test things out, but immediately off the top of my head, I'd recommend saving the text of field "Test" into some variable, and then using the split function - something like:
Code: Select all
put the text of field "Test" into testVar
split testVar by "/"
answer testVar[1] & " over & " testVar[2]
(note, I can't check but I think the text of field "Test" should do the trick, but I can never remember

)
The split function then converts it into an array that gets split at whatever you tell it to - in this case, "/"!
As I said, there's probably better ways to do it, and I'm sure there's an even better way I'm not thinking of because I'm not at my laptop, but this is the first thing that came to mind
Good luck - see how it goes!
Re: Split a Fraction into Words?
Posted: Fri Oct 09, 2020 3:14 pm
by elanorb
Hi Jon,
Words are always separated by one or more spaces, tabs, or returns, or enclosed by double quotes. What you need to do here is use "item".
You are already setting the itemDel to "/" so all you need to do is use item instead of word.
Code: Select all
on mouseUp
set the itemDelimiter to "/"
answer item 1 of field "Test"
end mouseUp
I hope that helps.
Elanor
Re: Split a Fraction into Words?
Posted: Fri Oct 09, 2020 3:18 pm
by cmhjon
Hi Elanor,
Thank you so much! That did the trick!
Best regards,
Jon

Re: Split a Fraction into Words?
Posted: Fri Oct 09, 2020 3:24 pm
by elanorb
Great! Glad I could help

Re: Split a Fraction into Words?
Posted: Fri Oct 09, 2020 4:10 pm
by dunbarx
Write it as a function of the fraction of interest:
Code: Select all
function changeToWords tFraction
set the itemDel to "/"
put "one two three four five six seven eight nine" into tWords
return word item 1 of tFraction of tWords && "over" && word item 2 of tFraction of tWords
end changeToWords
Craig