
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
Get two integers from the user, then create a two-dimensional array where the two dimensions have the sizes given by those numbers, and which can be accessed in the most natural way possible. Write some element of that array, and then output that element. Finally destroy the array if not done by the language itself.

set R to text returned of (display dialog "Enter number of rows:" default answer 2) as integer
set c to text returned of (display dialog "Enter number of columns:" default answer 2) as integer
set array to {}
repeat with i from 1 to R
	set temp to {}
	repeat with j from 1 to c
		set temp's end to 0
	end repeat
	set array's end to temp
end repeat
 
-- Address the first column of the first row:
set array's item 1's item 1 to -10
 
-- Negative index values can be used to address from the end:
set array's item -1's item -1 to 10
 
-- Access an item (row 2 column 1):
set x to array's item 2's item 1
 
return array
 
-- Destroy array (typically unnecessary since it'll automatically be destroyed once script ends).
set array to {}
 