Stuffing new numbers into a dummy list

This is my second post on the same subject but I may have been vague in my first post so I did not get any reaction.

Am new to AS scripting and need some way to get around a problem. I need something like an indexing method to fill up a list with some maximum value specified by the user. This maximum value also defines the length of the list. For example, if the user inputs a max value of 10, I also want the length of the list to be 10 items.

I have tried different ways but…I can’t seem to change the value of the list unless I set the value first and the length of the length at the start. Else, the list variable is “undefined” and I get an error message.

For example, I can define a list like: set some_List to {10, 15, 20, 30, 40}. It’s alright but that’s not what I want to do in the first place.

I am looking for something like the old variable indexing method and then set the values to the array items automatically in a repeat statement.

Is there a way to do this or some workaround the problem? I have found a way but it needs packing the list with numbers as text items, then parsing them into groups of digits that I want, then coercing the text back to numbers. But this is a long process and wasteful of code.

Please help. Thanks in advance.

:frowning:

I am really confused by your post. Is this what you are looking for?

set the_base to 5
set the_increment to 5
set list_length to 5
repeat
	try
		set list_length to text returned of (display dialog "Enter the list length:" default answer list_length buttons {"OK"} default button 1 with icon 1)
		set list_length to list_length as integer
		exit repeat
	end try
end repeat

set some_list to {}
repeat with i from 1 to list_length
	set the_base to (the_base + the_increment)
	set end of some_list to the_base
end repeat
return some_list

Jon

many thanks jon.

it looks like your suggestion it might work for my purpose. i will try it out and let you know if it did work.

:smiley:

> This is my second post... I may have been vague in my first post

> I need something like an indexing method to fill up a list with some
> maximum value specified by the user. This maximum value also
> defines the length of the list. For example, if the user inputs a max
> value of 10, I also want the length of the list to be 10 items.

  This is vague, sorry. A better example for the above would be to
show us exactly what the final list should look like after the user
indicates 10. Should every item be 10? Should it be a list of integers
from 1 to 10? By "indexing method," I think you want each item
of the list to be its own index, right?

    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

> I have tried different ways but...I can't seem to change the value
> of the list unless I set the value first and the length of the length
> at the start. Else, the list variable is "undefined" and I get an error
> message.

  It sounds like you may not be aware of how to append to a list:

    set a to {}
    set end of a to 1
    set end of a to 2
    set end of a to 3
    a --> {1, 2, 3}
or
    set a to {}
    repeat with i from 1 to 3
        set end of a to i
    end
    a --> {1, 2, 3}

  One can also use the "&" operator:

    set a to {1, 2, 3}
    set b to {4, 5, 6}
    set a to a & b
    a --> {1, 2, 3, 4, 5, 6}

  Here are some generic handlers for creating
new lists:

on SizeList(x, v)
	--
	--	Make new list of length x, where every item is v.
	--
	set a to {v}
	set i to 1
	repeat until (2 ^ i > x)
		set a to a & a --> exponential growth, very fast
		set i to i + 1
	end repeat
	set m to 2 ^ (i - 1)
	if (m = x) then
		return a
	else
		return a & a's items 1 thru (x mod m)
	end if
end SizeList

on ListIntegers(x, y, incr)
	--
	-- Create a list of integers from x to y,
	-- incremented by incr.
	--
	-- Both x and y can be negative. The resulting
	-- list will be FROM x TO y, even if y is
	-- smaller.
	--
	-- incr should be a positive integer, however,
	-- we will treat it as an absolute value, in
	-- case the user was unclear.
	
	set a to {}
	
	if (x = y) then return {x}
	
	-- Only one of these loops will actually execute.
	--
	repeat with i from x to y by incr
		set a's end to i
	end repeat
	repeat with i from x to y by -incr
		set a's end to i
	end repeat
	
	return a
	
end ListIntegers

SizeList(3, "Hi!") --> {"Hi!", "Hi!", "Hi!"}

ListIntegers(1, 3, 1) --> {1, 2, 3}
ListIntegers(3, 1, 1) --> {3, 2, 1}

ListIntegers(1, 10, 2) --> {1, 3, 5, 7, 9}
ListIntegers(2, 10, 2) --> {2, 4, 6, 8, 10}

ListIntegers(-3, 3, 1) --> {-3, -2, -1, 0, 1, 2, 3}
ListIntegers(3, -3, 1) --> {3, 2, 1, 0, -1, -2, -3}

dear admiral novia,

wow! what an informative reply to my post. thanks a lot. i learned a great deal from your reply.

i did try jon’s earlier suggestion and it worked (jon, this is for your update).

admiral, you were right i wanted to stuff the dummy list with the same numbers that the user would input (like the 10 as you mentioned) but that didn’t work very well for my purpose. i ended up using jon’s suggestion of incrementing each number and then stuffing them into the dummy list.

i will try your suggestion too and let you know later. you guys are really very very helpful and perhaps. have all the patience in the world to help us, neophytes in the AppleScript world! Thanks once more.

now my script works.

poggi11