Problems with creating lists

Hello

I wrote a script that generates “bi-dimensional” lists. I call bi-dimensional list a list like this:
{ {“a”,“b”,“c”} , {“d”,“e”,“f”} }
I associate such bi-dimensional list with a matrix like

| a d |
| b e |
| c f |

So, my bi-dimensioanl list is a 3 rows x 2 columns matrix.
The first item, {“a”,“b”,“c”}, is the first column; and the second item, {“d”,“e”,“f”}, is the second column.

So, the next script works fine:

set matrix to {{"", "", ""}, {"", "", ""}}  -- set an  3x2 "empty" matrix
set item 2 of item 1 of matrix to "b" 
matrix

It returns {{“”, “b”, “”}, {“”, “”, “”}}, just like I need.

But the script I wrote to generate the empty matrix generates something wrong.
After generating the “empty” matrix, setting a single element sets multiple elements.

My wrong thing is:

set rows to 2
set columns to 3
set matrix to generate_matrix(rows, columns)

set item 2 of item 1 of matrix to "b"
matrix

on generate_matrix(r, c)
	set empty_vector to {{""}}
	repeat c - 1 times
		set empty_vector to empty_vector & {{""}}
	end repeat
	set empty_row to empty_vector
	set empty_vector to {""}
	repeat r - 1 times
		set empty_vector to empty_vector & {""}
		end repeat
	set empty_column to empty_vector
	repeat with i from 1 to c
		set item i of empty_row to empty_column
	end repeat
	set matrix_vacia to empty_row
	return matrix_vacia
end generate_matrix

generate_matrix generates {{“”, “”, “”}, {“”, “”, “”}}
But when I try to set a single value, it is “propagated” thru every matrix element, and the result is {{“”, “b”, “”}, {“”, “b”, “”}} :frowning:
Well, i hope some one helps me.
Thanks in advance.

I make a mistake when copy-paste my wrong script.

Here is the correct version (that does’n work !!)

Hello

I wrote a script that generates “bi-dimensional” lists. I call bi-dimensional list a list like this:
{ {“a”,“b”,“c”} , {“d”,“e”,“f”} }
I associate such bi-dimensional list with a matrix like

| a d |
| b e |
| c f |

So, my bi-dimensioanl list is a 3 rows x 2 columns matrix.
The first item, {“a”,“b”,“c”}, is the first column; and the second item, {“d”,“e”,“f”}, is the second column.

So, the next script works fine:

set matrix to {{"", "", ""}, {"", "", ""}} -- set an 3x2 "empty" matrix
set item 2 of item 1 of matrix to "b" 
matrix

It returns {{“”, “b”, “”}, {“”, “”, “”}}, just like I need.

But the script I wrote to generate the empty matrix generates something wrong.
After generating the “empty” matrix, setting a single element sets multiple elements.

My wrong thing is:

set rows to 2
set columns to 3
set matrix to generate_matrix(rows, columns)

set item 2 of item 1 of matrix to "b"
matrix

on generate_matrix(r, c)
	set empty_vector to {{""}}
	repeat r - 1 times
		set empty_vector to empty_vector & {{""}}
	end repeat
	set empty_row to empty_vector
	set empty_vector to {""}
	repeat c - 1 times
		set empty_vector to empty_vector & {""}
	end repeat
	set empty_column to empty_vector
	repeat with i from 1 to r
		set item i of empty_row to empty_column
	end repeat
	set matrix_vacia to empty_row
	return matrix_vacia
end generate_matrix

generate_matrix generates {{“”, “”, “”}, {“”, “”, “”}}
But when I try to set a single value, it is “propagated” thru every matrix element, and the result is {{“”, “b”, “”}, {“”, “b”, “”}}
Well, i hope some one helps me.
Thanks in advance.

Perhaps something like this

set AppleScript's text item delimiters to {", "}

set myMatrix to emptyMatrix(2, 4)
display dialog myMatrix as string
set item 2 of item 1 of myMatrix to "a"
display dialog myMatrix as string

on emptyMatrix(r, c)
	
	set onecol to {}
	repeat r times
		copy "" to the end of onecol
	end repeat
	
	set returnMatrix to {}
	repeat c times
		copy onecol to the end of returnMatrix
	end repeat
	return returnMatrix
	
end emptyMatrix

.or this


set r to 2
set c to 3
set newMatrix to generate_matrix(r, c)
set item 2 of item 1 of newMatrix to "b"
newMatrix

on generate_matrix(rows_, columns_)
	set theMatrix to {}
	repeat columns_ times
		set aRow to {}
		repeat rows_ times
			set end of aRow to ""
		end repeat
		set end of theMatrix to aRow
	end repeat
	return theMatrix
end generate_matrix

Many many thanks, Mike and Stefan. Your scripts works as I need!! :slight_smile:

My above script filled emtpyMatrix with “”, this version fills it with {}.
Are there performace between the two?
How would I test for that?

on emptyMatrix(r, c)
	
	set onecol to {}
	repeat r times
		copy onecol to the end of onecol
	end repeat
	
	set returnMatrix to {}
	repeat c times
		copy onecol to the end of returnMatrix
	end repeat
	return returnMatrix
	
end emptyMatrix

-- BEGIN test routine
set myMatrix to emptyMatrix(2, 4)

set AppleScript's text item delimiters to {", "}
display dialog myMatrix as string

-- fill myMatrix with values
repeat with r from 1 to 2
	repeat with c from 1 to 4
		set item r of item c of myMatrix to ((r as text) & "." & (c as text))
		-- set item r of item c of myMatrix to 100 * r + c
	end repeat
end repeat

-- show myMatrix
display dialog myMatrix as string