Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to iterate/pick randomly from associative arrays?
Started by cindy.h1986 Sep 11 2013 02:08 PM

- - - - -
5 replies to this topic
arrays table sort random associative arrays

Best Answer johannes_lalala , 26 September 2013 - 03:44 AM

to clarify rob's answer:

 

local keys, i = {}, 1
for k,_ in pairs(t) do
 keys[i] = k
 i= i+1
end
-- then

local m
for i=1,5 do
   m= math.random(1,#keys)
   a[i] = t[ keys[m] ]
end

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

cindy.h1986

[GLOBAL: userInfoPane.html]
cindy.h1986
  • Contributor

  • 115 posts
  • Corona SDK

If I have...

local mRand = math.random
local t = {1,2,3,4,5,6,7,8,9}
local a = {}
for i=1, 5 do
     local num = mRand(1, #t)
     a[#a +1] = t[num]
end


But what if I have...
local t = { ["a"] = 1, ["b"] = 2, etc etc...  }
local a = {}
for i = 1, 5 do
      local num = mRand(1, #t)
     a[#a +1] = t[num]
end

This doesn't work...
How do I pick randomly from associative arrays?

[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,968 posts
  • Corona Staff

I would use a for k, v in pairs(t) loop and create an indexed loop where each array is the key from the table you care about, and then do a random number, look it up in the indexed table, get the key then get the value.



[TOPIC: post.html]
#3

johannes_lalala

[GLOBAL: userInfoPane.html]
johannes_lalala
  • Enthusiast

  • 49 posts
  • Corona SDK

  Best Answer

to clarify rob's answer:

 

local keys, i = {}, 1
for k,_ in pairs(t) do
 keys[i] = k
 i= i+1
end
-- then

local m
for i=1,5 do
   m= math.random(1,#keys)
   a[i] = t[ keys[m] ]
end


[TOPIC: post.html]
#4

cindy.h1986

[GLOBAL: userInfoPane.html]
cindy.h1986
  • Contributor

  • 115 posts
  • Corona SDK

Ok, I've put this aside for a bit and got a copy of Programming in Lua so I could read up on things.

 

I'll try that, thanks.

 

I read that, can't recall where, but that it's faster and uses less memory to do this(table inserts in general)

local mRand = math.random
local count = mRand(3,7) 
local t1 = { {"a", 1}, {"o", 1}, {"p", 2}, {"v", 2}, {"i", 1}, {"c", 5}, {"b", 1}, {"x",9}, {"w", 1}, {"m", 2}, {"e", 3} }
local t2 = {}
 
for i = 1, count do
     local val = mRand(1, #t1)
     t2[#t2 + 1] = t1[val]
end
 
print_r(t2)
 

 

another test...

local mRand = math.random
local count = mRand(3,7) 
local t1 = { {"a", 1}, {"o", 1}, {"p", 2}, {"v", 2}, {"i", 1}, {"c", 5}, {"b", 1}, {"x",9}, {"w", 1}, {"m", 2}, {"e", 3} }
local t2 = {}
local val = mRand(1, #t1)
 
for i = 1, count do
     t2[#t2 + 1] = t1[val]
end
 
print_r(t2)
 

 

another test...

local mRand = math.random
local count = mRand(3,7) 
local t1 = { {"a", 1}, {"o", 1}, {"p", 2}, {"v", 2}, {"i", 1}, {"c", 5}, {"b", 1}, {"x",9}, {"w", 1}, {"m", 2}, {"e", 3} }
local t2 = {}
 
for i = 1, count do
     t2[#t2 + 1] = t1[mRand(1, #t1)]
end
 
print_r(t2)
 

Though I noticed that sometimes a few entries are empty in my print_r( ), the empty ones show

 

[4]  => table: 0x10d4c34f0 {*table =0x112d23b60} etc.

 

It inserts a table but not the values inside, how come? is this method too fast? where did I go wrong with my randomization???

 

this btw doesn't give empty entries, however I do want to make the random thing work for my own sanity.... :)

local mRand = math.random
local count = mRand(3,7) 
local t1 = { {"a", 1}, {"o", 1}, {"p", 2}, {"v", 2}, {"i", 1}, {"c", 5}, {"b", 1}, {"x",9}, {"w", 1}, {"m", 2}, {"e", 3} }
local t2 = {}
 
for i = 1, count do
     t2[#t2 + 1] = t1[i]
end
 
print_r(t2)
 

 

Thanks for the help

 



[TOPIC: post.html]
#5

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

It's not empty. Tables are always passed by reference to it (address in memory they reside), so 0x10d4c34f0 is address of table. When you try to print table then there is no way to do it, instead lua will print it's address.

 

local t1 = { {"a", 1}, {"o", 1}, {"p", 2}, {"v", 2}, {"i", 1}, {"c", 5}, {"b", 1}, {"x",9}, {"w", 1}, {"m", 2}, {"e", 3} }

it is table of tables, so t1[1] = { "a", 1 }, then t1[1][1] = "a" and t1[1][2] = 1

 

 

In your second metod t2 will be always filled with the same elements - each element will be the same.



[TOPIC: post.html]
#6

cindy.h1986

[GLOBAL: userInfoPane.html]
cindy.h1986
  • Contributor

  • 115 posts
  • Corona SDK

Thanks, i ran a test in the simulator and all my entries did show up. I just got a bit confused because the terminal showed that. 

 

thanks.




[topic_controls]
[/topic_controls]