-1

Can some body help me to understand this piece of code?

local dev
for _, dev in ipairs(devices) do
        local net
        for _, net in ipairs(dev:get_wifinets()) do
                netlist[#netlist+1] = net:id()
                netdevs[net:id()] = dev:name()
        end
end
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 2
    Please tell us what parts of the code you don't understand and what parts you do so people can focus on that. – Mat May 04 '15 at 05:20
  • 4
    Emphasizing what @Josh pointed out, `local dev` and `local net` statements are less than not needed; They create unused variables. A `for` statement automatically creates its loop control variables. – Tom Blodget May 04 '15 at 12:21

1 Answers1

6

Broken down, it works like this.

local dev

Not really needed, but I'd imagine you know it creates local variable dev.

for _, dev in ipairs(devices) do

Loops through indexed table devices, and stores the value into dev locally.

local net

Again, not really needed.

    for _, net in ipairs(dev:get_wifinets()) do

dev uses the function get_wifinets(), which in turns returns an indexed table for which net loops through as the value.

            netlist[#netlist+1] = net:id()

This uses the id() function on the value net from the table returned with get_wifinets() and stores it as the last element in the netlist table.

           netdevs[net:id()] = dev:name()

Since we are still within the scope of the dev value loop, this takes the name() function of the dev value and stores it to the table listed, whether indexed or dictionary.

Hopefully this helps answer your question.

Josh
  • 3,225
  • 7
  • 30
  • 44