On Tue, Mar 12, 2013 at 10:29 PM, albert <albert_200200@126.com> wrote:
The example code is in the link:
http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-beta/ftp.html
1 require("ftp")
2 require("ltn12")
3 require("url")
4 function ls(u)
5 local t= {}
6 local p = socket.url.parse(u)
7 p.command = "nlst"
8 p.sink = ltn12.sink.table(t)
9 local r, e = socket.ftp.get(p)
10 return r and table.concat(t), e
11 end
I don't understand line 10, Does that mean "return (r and table.concat(t)),
e"?
r is a string, Then what does "r and table.concat(t)" mean?
It's what you wrote there, (r and table.concat(t)), e. It's using
boolean short-circuiting. A longer way to write it would be:
local val
if r then val = table.concat(t) end
return val, e
in both cases, if r is nil or false, the table.concat part of the
expression is ignored and the value returned is nil. socket.ftp.get
most likely returns (nil, error_message) on failure, so in that case
this function would also return (nil, e). It's comparable (but not
identical!) to C's ternary operator.