Split a Fraction into Words?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cmhjon
Posts: 190
Joined: Tue Aug 04, 2015 11:55 am

Split a Fraction into Words?

Post by cmhjon » Fri Oct 09, 2020 2:14 pm

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:

Image

If there is a better way to do this, I am all ears.

Thank you,
Jon :)

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Split a Fraction into Words?

Post by Opaquer » Fri Oct 09, 2020 3:11 pm

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 :P)

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!

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: Split a Fraction into Words?

Post by elanorb » Fri Oct 09, 2020 3:14 pm

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
Elanor Buchanan
Software Developer
LiveCode

cmhjon
Posts: 190
Joined: Tue Aug 04, 2015 11:55 am

Re: Split a Fraction into Words?

Post by cmhjon » Fri Oct 09, 2020 3:18 pm

Hi Elanor,

Thank you so much! That did the trick!

Best regards,
Jon :)

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: Split a Fraction into Words?

Post by elanorb » Fri Oct 09, 2020 3:24 pm

Great! Glad I could help :)
Elanor Buchanan
Software Developer
LiveCode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Split a Fraction into Words?

Post by dunbarx » Fri Oct 09, 2020 4:10 pm

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

Post Reply