nil reference

Hi,

I’m getting confused by the results of the loop below:

  local param={     path=path,     baseDir=baseDir,     isAbsolute=isAbsolute   }   local dir=derivePath(param)     local setups={}   for file in lfs.dir(dir) do     local folderPath=dir.."/"..file     if file~=".." and file~="." and lfs.chdir(folderPath) then       local config=jsonreader.load(folderPath.."/task.json")       if config then         setups[#setups+1]={file=file,searchParam=param}         assert(param)       end     end   end   print ("Listed",serpent.block(setups),{comment=false})

For a reason I don’t understand the print out at the end of the example prints the following:

Listed {

  {

    file = “one”,

    searchParam = {

      isAbsolute = true,

      path = “valid/file/path”

    }

  },

  {

    file = “two”,

    searchParam = nil

  },

  {

    file = “three”,

    searchParam = nil

  },

  {

    file = “four”,

    searchParam = nil

  }

}

I don’t see how each of the subsequent search param fields can be nil after the first is set. If param was set to nil the assert would trigger. I’ve added printout to display the value of param, and it does not change during the loop. Each iteration it remains

{   isAbsolute = true,   path = "valid/file/path" }

Yet somehow all the searchParam fields bar the first one are set to nil by the time the loop complete! 

I can fix the nil references by changing the line

setups[#setups+1]={file=file,searchParam=param}

to 

setups[#setups+1]={      file=file,      searchParam=\_.clone(param) }

Where _ is the moses library, the fix also works if I copy the values from param into a new table and attach that to the table being stored in the setups list.

I still would like to know what is nilling the values in my list though. Any ideas?

I can fix the nil references by changing the line

setups[#setups+1]={file=file,searchParam=param}

to 

setups[#setups+1]={      file=file,      searchParam=\_.clone(param) }

Where _ is the moses library, the fix also works if I copy the values from param into a new table and attach that to the table being stored in the setups list.

I still would like to know what is nilling the values in my list though. Any ideas?