这条LUA线是什么意思? “”%i(%。3f GHz)“%{f.channel,f.mhz / 1000})”

时间:2013-04-05 14:13:39

标签: lua

我对这段代码有疑问

ch = s:taboption("general", Value, "channel", translate("Channel"))
ch:value("auto", translate("auto"))
for _, f in ipairs(iw and iw.freqlist or { }) do
    if not f.restricted then
       ch:value(f.channel, "%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 }) //this line?
    end
end

该做什么?

ch:value(f.channel, "%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 })

我认为它为ch赋予了一些价值,但我无法理解哪一个,因为我无法完全理解这部分的内容“%i(%。3f GHz)”%{f.channel,f.mhz / 1000}

提前致谢!

完整代码

-- Check whether there is a client interface on the same radio,
-- if yes, lock the channel choice as the station will dicatate the freq
local has_sta = nil
local _, net
for _, net in ipairs(wdev:get_wifinets()) do
    if net:mode() == "sta" and net:id() ~= wnet:id() then
        has_sta = net
        break
    end
end

if has_sta then
    ch = s:taboption("general", DummyValue, "choice", translate("Channel"))
    ch.value = translatef("Locked to channel %d used by %s",
        has_sta:channel(), has_sta:shortname())
else
    ch = s:taboption("general", Value, "channel", translate("Channel"))
    ch:value("auto", translate("auto"))
    -- for _, f in ipairs(iw and iw.freqlist or { }) do
    for _.iw.chanlist, f in ipairs(iw and iw.freqlist or iw.channels) do
        if not f.restricted then
            ch:value(f.channel, "%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 })
        end
    end
end

2 个答案:

答案 0 :(得分:3)

看起来%运算符是通过字符串'metatable

__mod metamethod为字符串定义的
getmetatable''.__mod = function(str, tbl)
   return str:format((table.unpack or unpack)(tbl))
end

答案 1 :(得分:1)

"%i (%.3f GHz)" %{ f.channel, f.mhz / 1000 }
  1. %i将参数解析为整数。
  2. %.3f将第二个参数格式化为带有3个小数位的浮点数。
  3. %{}定义传递给前一个字符串以进行格式化的参数。
    • 第一个参数是f.channel
    • 第二个参数是f.mhz/1000

  4. ch:value( f.value, ... )
    

    与以下内容相同:

    ch.value( ch, f.value, ... )