Splitting number into digits?

Is there an easy way to split a large number into separate digits?

If the number is say, “987654321” I want to set variable1 to 9, variable2 to 8, variable3 to 7, etc.

I know how to do this with the integer command and a complex forumla, but does applescript provide an easier way?

Thanks,
Torajima

Is this close to what you are looking for?

set big_num to every character of "987654321" as list
-->{"9", "8", "7", "6", "5", "4", "3", "2", "1"}

You now have a list, in order, of all the digits, and instead of assigning each to a different variable, you can simply call [item 3 of big_num] or something like that.

Thanks! That’s exactly what I needed…

Torajima

Oops, I spoke to soon. It works by itself, but when I try to use an answer rather than a set number it doesn’t work. Obviously, I’m doing something wrong.

display dialog "Enter Number" default answer "" buttons {"OK"} default button 1

set anumber to result
set numberlist to every character of anumber as list

set digit2 to [item 2 of numberlist]

You are very close:




set numberlist to every character of (text returned of (display dialog "Enter Number" default answer "" buttons {"OK"} default button 1))

set digit2 to (item 2 of numberlist) as number

I took the liberty of setting your digit2 variable to a number. If you simple delete the [as number] portion, it will remain as a string value, and you can change it to a number later if you want.

Thanks, but the problem is this: I need the original value (the whole number) and the individual digits. That’s why I set the results to anumber and tried to make a list from that.

Hi Torajima,

Try this:

display dialog "Enter Number" default answer "" buttons {"OK"} default button 1

set anumber to text returned of result
set numberlist to every character of anumber as list

set digit2 to (item 2 of numberlist)

Your script wasn’t working because you were getting the result of the display dialog (an array of {text returned, button returned}), not the text returned of the result.

Try this script and look at the result pane in Script Editor:

display dialog "Enter Number" default answer "" buttons {"OK"} default button 1

set anumber to result

Best wishes

John M

Thanks guys, that should do the trick.

Torajima

I have a check that I add to scripts that need number input that I thought I would share. It will make sure the user inputs a positive whole number. Just in case you want to make it more “user-friendly”. This will also give you a new variable called “anumberinteger” that converts the text input back to a real number in case that’s helpful.


set anumber to text returned of (display dialog "Enter Number" default answer "" buttons {"OK"} default button 1)

try
set anumberInteger to anumber as integer
on error
display dialog "Expected number entry. Cancel and try again" buttons "Cancel" default button "Cancel"
end try
if anumberInteger is not greater than 0 then
display dialog "Invalid number. Cancel and try again" buttons "Cancel" default button "Cancel"
end if

set numberlist to every character of anumber as list

set digit2 to (item 2 of numberlist)


Model: Mac G5 OS 10.3.9
Operating System: Mac OS X (10.3.9)