# a question about if/then and lists..

hi,

i’ll post this and hope it explains what i’m after…

``````

if Variable = {1, 2, 3, 4, 5} then
set NewVar to {"a", "b", "c", "d", "e"}
``````

desired results would be if Variable = 4 then NewVar = “d” …etc
basically looking for the ‘proper’ way to script that or do i just have to do a long line of if/then/else (which is how i’m doing it now )

thank you

Hi,

if - else - end if is not necessary in this case.
Variable can be used as an index to get the respective item of the list

``````
set NewVar to item Variable of {"a", "b", "c", "d", "e"}

``````

ha!
nice
thank you.

that happened to work well for what i’m doing right now but i see a similar case down the line where it might be something like:

is there any way to deal with that other than

(which i guess wouldn’t be all that bad for me to write if it were just 5 possibilities… i might have up to 18 though)

thanks!

the shortest way is a repeat loop

``````
property indexList : {5, 10, 20, 50, 100}
property valueList : {"a", "b", "c", "d", "e"}

set NewVar to missing value
set variable to 20
repeat with i from 1 to count indexList
if variable is item i of indexList then
set NewVar to item i of valueList
exit repeat
end if
end repeat
if NewVar is missing value then
-- error handling if indexList does not contain variable
else
-- do something with newVar
end if

``````

ah… ok… thank you…

fwiw, i’m trying to display some fractions from decimal inches (fractions of an inch rounded to the nearest 16th)… there’s a decently neat way to do this in numbers.app but applescript is seemingly lacking in operators(?)

this is how i’m doing it now with the help of your tip but maybe there’s a better way?

``````

set decInch to 0.63453
set sixteenths to round (16 * decInch)

if sixteenths mod 2 is not 0 then
set fraction to (sixteenths as text) & "/16"
else
set fraction to item sixteenths of {"", "â…›", "", "¼", "", "â…œ", "", "½", "", "â…", "", "¾", "", "â…ž"}
end if

``````

the decInch varies and is being defined by some calculations earlier in the script… it’s generally a .00000 #…

i’m still not sure about the text substitutions using ¼ etc when applicable but falling back on the x/16 in other spots… but i imagine i’ll eventually make them all “1/16”, “1/8”, etc

anyway, if you know of any better ideas for handling this stuff, i’m all ears
thanks
jeff