1. #1

    [expert level] naming variables

    Ok, here's the problem. I want to store dynamically a sequence of values in a table i already have created. The idea is to make a standard function that updates these variables without defining them one by one - until now i had like 2-3 of them, now we're talking about 40-50.

    What i have and can use? I have a list = {a, b, c, d} which is already defined, i want to do a method to create new fields in a "database" called with the names in that list.

    The method should look like this:
    Code:
    function storevalues()
      for _, v in pairs(list) do
         local db = "my database"
         fieldname = _G[v]
         -- create the field called db.fieldname
         if somekindofcondition then 
           db.fieldname = true
         else
           db.fieldname = false
         end
      end
    so the result should be something like
    Code:
    db.a = true
    db.b = false
    db.c = true
    db.d =true
    Making manually a variable for every value i need to store isn't an option, since i can even make it, but if i change something i'd need to double-check every one.
    Non ti fidar di me se il cuor ti manca.

  2. #2
    I'm not entirely sure what you are trying to do, but here's a snippet I think will help:

    Code:
    local list, database = {"a","b","c","d"}, {};
    
    function StoreDatabaseValues()
    	for k, v in pairs(list) do
    		database[v] = true; -- set whatever value
    	end
    end
    
    
    function GetDatabaseValue(key)
    	return database[key];
    end

  3. #3
    Deleted
    At first glance:
    db[fieldname], not db.fieldname - the latter is equal to db['fieldname'].

  4. #4
    Thanks for help, managed to do the trick

    result: http://i.imgur.com/nsPJp.jpg

    (made movable and togglable frames)
    Non ti fidar di me se il cuor ti manca.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •