为什么`i`总是在for循环中被视为1

时间:2012-07-19 08:25:46

标签: for-loop lua string-formatting increment

我写了这个简单的脚本来更新MySQL中的表。为此,我创建了一个for循环并尝试了以下(codepad link):

sUpdate = [[UPDATE `latest`
SET `date` = '%s'
WHERE `date` = '%s'
AND `fid` > 50000]]

for i = 1, 12 do
  print( i )
  sOldDate = "2009-"..tostring(i).."-10"
  sNewDate = "2010-09-"..tostring(i)
  sUpdate = string.format( sUpdate, sNewDate, sOldDate )
  print( sUpdate )
end

输出如下:

1
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
2
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
3
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
4
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
5
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
6
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
7
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
8
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
9
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
10
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
11
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
12
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000

正如您所看到的,print(i)打印得很好,但sOldDatesNewDate都被视为 1 。然后我更改了sOldDatesNewDate,如下所示:

sOldDate = string.format("2009-%d-10", i)
sNewDate = string.format("2010-09-%d", i)

我仍然得到两个日期的输出: 2009-1-10 2010-09-1 ,如here所示。

这个循环可能有什么问题。我已经做了很长时间的这种循环,他们从来没有让我失望,直到今天。

我认为这只是我的一些愚蠢的错误,我无法识别。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:4)

是的,问题是你每次都要覆盖sUpdate

第一次,您将覆盖字符串占位符%s,之后,字符串不会再次更改。

尝试重命名内部sUpdate。而且我认为您希望所有变量都为local

local sUpdate = [[UPDATE `latest`
SET `date` = '%s'
WHERE `date` = '%s'
AND `fid` > 50000]]

for i = 1, 12 do
  print( i )
  local sOldDate = "2009-"..tostring(i).."-10"
  local sNewDate = "2010-09-"..tostring(i)
  local sUpdate = string.format( sUpdate, sNewDate, sOldDate )
  print( sUpdate )
end

编辑:正如您在上面所看到的,我保留了您的变量名称,但内部sUpdate不会影响外部变量名称,因为它已声明为local。默认情况下,Lua中的所有变量都是全局变量,因此最好确保使用local声明局部变量。我会选择使用不同的变量名称,例如:

local sUpdateTemplate = [[UPDATE `latest`
SET `date` = '%s'
WHERE `date` = '%s'
AND `fid` > 50000]]

for i = 1, 12 do
  print( i )
  local sOldDate = "2009-"..tostring(i).."-10"
  local sNewDate = "2010-09-"..tostring(i)
  local sUpdate = string.format( sUpdateTemplate, sNewDate, sOldDate )
  print( sUpdate )
end

答案 1 :(得分:4)

如上所述,您需要在循环内重命名sUpdate以避免覆盖外部的sUpdate并破坏string.format()。然后,您可以在string.format()中使用%02d将数字填充到零至少两位数字:

local sUpdate = [[UPDATE `latest`
SET `date` = '%s'
WHERE `date` = '%s'
AND `fid` > 50000]]

for i = 1, 12 do
  print( i )
  local sOldDate = string.format("2010-%02d-10", i)
  local sNewDate = string.format("2010-09-%02d", i)
  local update = string.format( sUpdate, sNewDate, sOldDate )
  print( update )
end