giving multiple variables the same value at once

Is there a quick/shorthand way for giving multiple variables the same value at once?

I have a 2d array that I want the value 1 stored in essentially random places, so I cannot use a loop.
As far as I know I will have to input each value in one at a time:

grid[4][7] = 1
grid[4][8] = 1
grid[6][2] = 1

etc…

Thanks!
Dane
[import]uid: 117490 topic_id: 20679 reply_id: 320679[/import]

You could do it within a loop and use a random function in each iteration to give a 25% chance of whatever that that element of the array should be set to 1?

[import]uid: 93133 topic_id: 20679 reply_id: 81210[/import]

There are many ways of doing this, the loop below places 1 to 10 ones in a 2D array forcing the loop to reiterate when duplicate sets of indices are found. I did not test this snippet to make sure it is error free.

[code]
local number_of_ones = math.random(1,10)
local row_limit = 10
local col_limit = 10
local i
for i=1,number_of_ones do
row = math.random(1,row_limit)
col = math.random(1,col_limit)

if grid[row][col] == 1 then
i = i - 1
else
grid[row][col] = 1
end
end
[/code] [import]uid: 100558 topic_id: 20679 reply_id: 81272[/import]

Thanks guys, but:

I didn’t really intend “random” I just mean that there is no organized pattern to their locations (e.g. the walls of a maze).

I’d still be interested to know if there is syntax allowing you to take a handful of variables at once and set them equal to the same value, for example:

x,y,coins,health = 5

Does anybody know syntax like that? Thanks.

At any rate, I woke up middle of the night and realized in my particular situation I have enough stuff to enter that it would be easier for me to fill in the tables as I define them. As opposed to doing a for loop to set a bunch of zeros and then manually entering in the 1’s. (I am looking off the level images to determine where the numbers go).

[import]uid: 117490 topic_id: 20679 reply_id: 81274[/import]

At best you could do:

x,y,coins,health = 5,5,5,5

Otherwise, if you store all of your variables as members of a table you could push the keys names into another table and iterate through that to set all of your variables.

[code]
– Initialization
local var_table =
{
var1 = 0,
var2 = 0,
var3 = 0,
y = 0,
x = 0,
coins = 0,
health = 0,
}

local key_table = {}


– function that picks variables to set

key_table[#key_table + 1] = “x”
key_table[#key_table + 1] = “y”
key_table[#key_table + 1] = “coins”
key_table[#key_table + 1] = “health”


– function that sets the variables to 1

for _,key in ipairs(key_table)
var_table[key] = 1
key_table[key] = nil --clear the key out of the table as you use it
end
[/code] [import]uid: 100558 topic_id: 20679 reply_id: 81288[/import]