lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 22 January 2015 at 07:29, Gordon Madarm <gmadarm@gmail.com> wrote:
I'm trying to understand how the table of tables works in the params example given here:
https://github.com/pintsized/lua-resty-http#request_pipeline

Why doesn't the value for path get overwritten with each new instance? How can I dynamically create a table of param tables to use with this method, e.g. I want to read the contents of an external file containing the following and create the table for the request_pipeline method:

/pathTo/request1.php
/pathTo/request2.php
/pathTo/request3.php
/pathTo/request4.php

thanks,
- G

Why would path be overwritten? You have one outer table being passed to request_pipeline. This table contains three values at keys 1, 2 and 3; each of which is a unique table with a string at key "path". These inner tables have no effect on each other.

This is an example of how you'd create the table for request_pipeline:

-- Assumes that file_with_paths.txt contains one path on each line

local request = {} -- The request table to pass to httpc:request_pipeline
local currentKey = 0 -- The most recent key an inner params table was stored at

for line in io.lines("file_with_paths.txt") do
currentKey = currentKey + 1 -- Increment currentKey
request[currentKey] = { path = line } -- Store a params table at currentKey
end

httpc:request_pipeline(request) -- Make the request


Regards,
Choonster