Text Item Delimiters - Help

I have data like 1-2/15, 3-11/13, 7/23, 5-1/6 . . . I need to get the components of fractions as variables.
Example: if the value is 1-2/15, then i want to have 1 as WholeNumber variable, 2 as Numerator variable & 15 as Denominator variable.

I was able to get the variables for fraction “7/23”, I am bit lost in getting the variables for the mixed fractions (i.e 1-2/15). Any advise will be much appreciated.


set fractionNum to "7/23" <----- if this is 1-2/15, i am not sure, how to handle this, need assistance here.
set AppleScript's text item delimiters to "/"
set numberInch to get every text item of fractionNum as list
set numerator to get 1st text item of numberInch
set AppleScript's text item delimiters to ""
set denominator to 2nd item of numberInch
display dialog numerator
display dialog denominator

Thanks in advance
JaiM

Something like this??

set frac to "1-13/15"
set O to offset of "-" in frac
set P to offset of "/" in frac
tell frac
	if O is not 0 then
		set whole to characters 1 thru (O - 1) as text
	else
		set whole to 0
	end if
	set Num to items (O + 1) thru (P - 1) as text
	set den to items (P + 1) thru -1 as text
end tell
{whole, Num, den} --> {1, 13, 15}

I didn’t test it for a missing fraction, but it will probably barf.

First I’l comment on your code which may help you understand text item delimiters a little better…

set numberInch to get every text item of fractionNum as list
Invoking text items returns a list of items, so you do not need to coerce the result “as list”. It’s already a list.

set numerator to get 1st text item of numberInch
Since numberInch is a list it has components called item 1, item 2 and so on. So this line should be “item 1 of numberInch” or “first item of numberInch”. When you say “first text item” you are invoking “text items” again and thus are trying to turn the items in the numberInch list into more lists rather than getting the actual items of numberInch… and of course you’re not trying to make more lists.

Now here’s how I would use text item delimiters to solve your question…

set myData to "1-2/15, 3-11/13, 7/23, 5-1/6, 12"

set text item delimiters to ", "
set numbersList to text items of myData --> {"1-2/15", "3-11/13", "7/23", "5-1/6", "12"}

set finalList to {}
repeat with aNumber in numbersList
	set {wholeNumber, numerator, denominator} to {missing value, missing value, missing value}
	if aNumber contains "-" then
		set text item delimiters to "-"
		set thisList to text items of aNumber
		set {wholeNumber, fraction} to {item 1 of thisList, item 2 of thisList}
		if aNumber contains "/" then
			set text item delimiters to "/"
			set fractionList to text items of fraction
			set {numerator, denominator} to {item 1 of fractionList, item 2 of fractionList}
		end if
	else if aNumber contains "/" then
		set text item delimiters to "/"
		set fractionList to text items of aNumber
		set {numerator, denominator} to {item 1 of fractionList, item 2 of fractionList}
	else
		set wholeNumber to aNumber
	end if
	
	if wholeNumber is not missing value then set wholeNumber to wholeNumber as number
	if numerator is not missing value then set numerator to numerator as number
	if denominator is not missing value then set denominator to denominator as number
	set end of finalList to {wholeNumber:wholeNumber, numerator:numerator, denominator:denominator}
end repeat
set text item delimiters to ""
return finalList
--> {{wholeNumber:1, numerator:2, denominator:15}, {wholeNumber:3, numerator:11, denominator:13}, {wholeNumber:missing value, numerator:7, denominator:23}, {wholeNumber:5, numerator:1, denominator:6}, {wholeNumber:12, numerator:missing value, denominator:missing value}}

Adam Bell,
Excellent. Thanks for the quick reply and suggestion. Your code did help me to get the solution that i was looking for. Once again Thanks a Million.

Regulus,
You are Super Star and Genius. Really appreciate your patience in analyzing my code and assist me in understanding the text item delimiters. Thanks a Million. Your suggestion also help me to get the solution that i was looking for. Thanks a Million.

Rgds
JaiM