create list of number betwwen 2 given number (newbie)

Hi, i’m new to this forum
My goal is to have a script which create the list of number between two given number

For example if i have
x=6 , y=10
the list should be: {6 , 7 , 8 , 9 , 10}

Any help is gratly apreciated!
Thank you, Stefano

Browser: Firefox 1.5.0.3
Operating System: Mac OS X (10.4)

Hi Steve,

You could do something like:

set x to 6
set y to 10
set myResult to {}

repeat with z from x to y
	set end of myResult to z
end repeat

return myResult

Best wishes

John M

hi steve,

this should do it:


set x to 6
set y to 10
set myList to {}

repeat with x from x to y
	set end of myList to x
	x = (x + 1)
end repeat

This should get you started:

set x to text returned of (display dialog "Enter first number" default answer "1" buttons {"Cancel", "OK"} default button "OK")

set y to text returned of (display dialog "Enter second number" default answer "10" buttons {"Cancel", "OK"} default button "OK")

set theList to {}
repeat with z from x to y
	set end of theList to z
end repeat

NOTE: I haven’t done any error handling here. This script assumes that 2 numbers are entered AND that the first number is less than the second number. Can’t give you everything.:wink:

Brad Bumgarner, CTA

Thank’s a lot, the script works fine :smiley:

just another thing, what if the list should be { 01 , 02 , 03 etc}

Thank to all you
Hope i can give help in future

The list will have to be text to have leading zeros (AppleScript will drop them from integers):

set myList to {2, 3, 14, 5}
set newList to {}
repeat with N in myList
	set end of newList to pad(N, 3)
end repeat
newList --> {"002", "003", "014", "005"}

on pad(num, howLong)
	set thisNum to num as text
	set c to count thisNum
	repeat howLong - c times
		set thisNum to "0" & thisNum
	end repeat
	return thisNum
end pad

Here’s another option (that doesn’t do zero padding):

makeSequenceList from "1" to "3"

to makeSequenceList from min to max
	do shell script "/usr/bin/jot " & ¬
		quoted form of (max - min + 1 as text) & space & ¬
		quoted form of min & space & ¬
		quoted form of max
	return paragraphs of result
end makeSequenceList

Doesn’t work for me, Bruce - it returns “1” and “3”.

I edited my script above.

Thanks - Nice.

I saw trying to use sed to zero pad single digit numbers, but it wasn’t working.

hey guys

this is not directly to do with this script so i apologise in advance,
i’m very new to scripting and just picking it up a piece at a time,
the piece today would be this,
most of the scripts above except bruces, set mylist,thelist, or theresult to{} an empty list
my question is why do you after do this?
i understand the script doesn’t work to good with out it,
just not sure when i’d have to put one of these little snippets in a script,

thanks

To manipulate a list it has to exist first. A declaration like set newList to {} defines the variable “newList” as an empty list. Later you can add to it. Same is true of a string variable - to build it, you first have to declare it.

In this, for example:

set myList to {2, 3, 14, 5}
set newList to {} -- establish a list by this name
repeat with N in myList
	set end of newList to pad(N, 3) -- now add stuff to it. If it had not been defined above, what would "end of newList" mean?
end repeat

If we wanted the alphabet in capital letters:

set myText to ""
repeat with k from 65 to 90
	set myText to myText & (ASCII character k)
end repeat
myText --> "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

thanks adam
that makes it abit clearer…

I added a second thought about strings.

its the same deal with a string as it is with a list then i take it…

i’ve still got a lot to learn!!!

Here’s a development which does. It needs further refinement, though, and there’s probably a better way anyway that shell scripters will know about. It works up to a padding width of 8 digits. The ‘onto’ parameter controls the width. (0 to 8.) A zero ‘onto’ value gives no padding at all. 1 is a waste of time.

makeSequenceList from 1 to 17 onto 2
--> {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17"}

to makeSequenceList from min to max onto width
	if (width > 0) then
		set p to (10 ^ width) as integer
		if (p is not greater than max) then
			display dialog "makeSequenceList" & return & return & "One or more numbers in the sequence " & min & " to " & max & " need more than " & width & " digits." buttons {"OK"} default button 1 with icon stop
			error number -128
		end if
		set min to (min + p - 1) as text
		set max to (max + p) as text
	end if
	set s to (do shell script "/usr/bin/jot " & ¬
		quoted form of (max - min + 1 as text) & space & ¬
		quoted form of min & space & ¬
		quoted form of max)
	if (width > 0) then
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to return & "1"
		set s to rest of s's text items
		set AppleScript's text item delimiters to astid
	else
		set s to s's paragraphs
	end if
	
	return s
end makeSequenceList

It adds a power of 10 to ‘min’ and ‘max’ so that (in the above example) the shell script produces numeric paragraphs from “101” to “117”. TIDs are then used to separate the paragraphs and remove the leading "1"s. A special arrangement is needed to zap the leading “1” from the first paragraph. I’ve chosen to ‘min’ set to 1 less than is actually required, so that an extra number is generated at the front of the list, which is then simply dropped.