Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to store some values for each string element in Lua array.

-- Emulating different Browsers
local user_agent = {
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",         
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36",          
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36",                   
"Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0",                                                                                  
"Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0",                                                               
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; FunWebProducts)",                                                          
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; InfoPath.1; SV1; .NET CLR 3.8.36217; WOW64; en-US)",               
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"  
}

-- Number of connections per host and total connections for each browser/user_agent
 user_agent[1].max_conn_perhost , user_agent[1].max_conn_total = 6, 17
 user_agent[2].max_conn_perhost , user_agent[2].max_conn_total = 6, 10
 user_agent[3].max_conn_perhost , user_agent[3].max_conn_total = 6, 10
 user_agent[4].max_conn_perhost , user_agent[4].max_conn_total = 6, 16
 user_agent[5].max_conn_perhost , user_agent[5].max_conn_total = 6, 16
 user_agent[6].max_conn_perhost , user_agent[6].max_conn_total = 6, 35
 user_agent[7].max_conn_perhost , user_agent[7].max_conn_total = 6, 35
 user_agent[8].max_conn_perhost , user_agent[8].max_conn_total = 6, 16

This is throwing error:

attempt to index field '?' (a string value)

I have noticed in some examples that, if I haven't initialized the string array, then it will work. Can anyone please suggest any easier solution to achieve this or rectify the issue.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

From what you've posted, you have a string array and want to index its elements; this code has no chance of working whatsoever:

t = { "foo", "bar" }

-- t[1] is "foo"
-- t[1].xyz is the same as t[1]["xyz"], which evaluates to "foo"["xyz"], which is probably not what you want

What you need is an array of "objects":

t = { {"foo"}, {"bar"} }

t[1].xyz = 5 -- works

However, "foo" will be under index 1, so you probably will want to assign a name for it

t = { {name="foo"}, {name="bar"} }
share|improve this answer
    
Thanks a lot for explaining the issue and solution! –  Kumar Vikramjeet Jul 9 '13 at 5:12

Insert these lines after declaring user_agent, but before assigning to max_conn_perhost and max_conn_total:

for i, name in ipairs(user_agent) do
    user_agent[i] = {name = name}
end
share|improve this answer
    
Thanks, for this brief solution! Can you please explain what the second line is achieving? Is it typecasting elements of array into objects? –  Kumar Vikramjeet Jul 9 '13 at 5:13
    
@KumarVikramjeet - It is replacing array elements (strings) with objects containing these strings. –  Egor Skriptunoff Jul 9 '13 at 5:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.