【发布时间】:2011-03-12 17:56:44
【问题描述】:
我需要把这段代码从 Perl 翻译成 Lua
open(FILE, '/proc/meminfo');
while(<FILE>)
{
if (m/MemTotal/)
{
$mem = $_;
$mem =~ s/.*:(.*)/$1/;
}
elseif (m/MemFree/)
{
$memfree = $_;
$memfree =~ s/.*:(.*)/$1/;
}
}
close(FILE);
到目前为止,我已经写了这篇文章
while assert(io.open("/proc/meminfo", "r")) do
Currentline = string.find(/proc/meminfo, "m/MemTotal")
if Currentline = m/MemTotal then
Mem = Currentline
Mem = string.gsub(Mem, ".*", "(.*)", 1)
elseif m/MemFree then
Memfree = Currentline
Memfree = string.gsub(Memfree, ".*", "(.*)", 1)
end
end
io.close("/proc/meminfo")
现在,当我尝试编译时,我收到关于我的代码第二行的以下错误
luac: Perl to Lua:122: unexpected symbol near '/'
显然,在 string.find 中使用目录路径的语法与我编写的方式不同。 “但是怎么样?”是我的问题。
【问题讨论】: