Getting Numbers in Specific Increments

I’m simply trying to get a list of every number from 0 through 15,000 by increments of 50, as in {0, 50, 100, 150, 200, 250} etc. I don’t think I’m going about it the right way. I’m sure it’s much simpler than I am making it to be, anyone have a push in the right direction? Thanks for any help. :slight_smile:

Would something like this help?

do shell script "/usr/bin/jot - 0 15000 50"
return paragraphs of result

Bruce, awesome. Thanks a bunch! :slight_smile:

If you are going to use the list as integers rather than text you may want to try this instead.

set my_list to {}
repeat with idx from 0 to 15000 by 50
	copy idx to end of my_list
end repeat

While the “do shell script” example is wicked fast on it’s own, coercing 301 items of a list to integer will affect performance. Although I am splitting hairs. I’m not saying this is the right way, just another way.

Depending on what you’re doing, you could coerce the text version when you need to. Also, you could just run my first script in Script Editor, then take out the quotes, and paste it into your script.

Speaking of speed, this was much faster on my computer (as tested with GetMilliSec):

set theList to {}
repeat with i from 0 to 15000 by 50
		set theList's end to i
end repeat

More specifically, when repeating our scripts 10 times, yours averaged 230 ms and mine averaged 30 ms. I guess copying an object 301 times affects performance. :wink:

Edit: For the curious, running my first script once and then coercing to integer took ~186 ms.

This is what I tested Jacques.

~30 milliseconds

set t1 to GetMilliSec

repeat 10 times
	set theList to {}
	repeat with i from 0 to 15000 by 50
		set theList's end to i
	end repeat
end repeat

set t2 to GetMilliSec

t2 - t1

~230 milliseconds

set t1 to GetMilliSec

repeat 10 times
	set my_list to {}
	repeat with idx from 0 to 15000 by 50
		copy idx to end of my_list
	end repeat
end repeat

set t2 to GetMilliSec

t2 - t1

~186 milliseconds

set t1 to GetMilliSec

do shell script "/usr/bin/jot - 0 15000 50"
set theList to paragraphs of result
count result

repeat with i from 1 to result
	set theList's item i to (theList's item i) as integer
end repeat

set t2 to GetMilliSec

t2 - t1

FWIW, I was getting 4.7 seconds for that first script you mentioned.

A slip of the fingers, no doubt, Bruce - but that acts on only the first item in the list. It would take slightly longer to coerce all the items in the list with something like:


set l to paragraphs of (do shell script "/usr/bin/jot - 0 15000 50")
repeat with i from 1 to count l
	set l's item i to l's item i as integer
end repeat
l


An alternative form of coercion should prove faster than either of the above versions (in spite of the multiple assignments used here):


set {d, text item delimiters} to {text item delimiters, ", "}
set {l, text item delimiters} to {run script "{" & (do shell script "/usr/bin/jot - 0 15000 50")'s paragraphs & "}", d}
l


However, referencing (using either the following method or, in a handler, a script object) improves the efficiency of the repeat loop significantly:


set l to paragraphs of (do shell script "/usr/bin/jot - 0 15000 50")
repeat with i from 1 to count l
	set my l's item i to my l's item i as integer
end repeat
l


My general preference for this type of job, though, would still be vanilla AS - since it generates numbers directly. Even if text values are required, it’s no slouch:


set l to {}
repeat with i from 0 to 15000 by 50
	set l's end to i
end repeat
set d to text item delimiters
set text item delimiters to return
set l to (l as text)'s paragraphs
set text item delimiters to d
l


(Incidentally, the copy command is slower than set - and is normally used to avoid data sharing between objects such as lists, records, dates and script objects.)

I had it right when I timed it. :stuck_out_tongue: The typo came when I retyped it so I could post it.

Neat way to convert a list of numbers to a list of those numbers as text, but not in reverse (to get numbers from text)

Take another look at my second example above, Adam (starting “An alternative form of coercion…”). :wink: