0

i have some problem in my array2d. i actually want plan to have refresh command.

my data on data.txt

test1:30:1
test2:40:2

So whenever i call function ReadData. It will send to my Array2d something like this :

line_data = {{"test1", "30", "1"},
             {"test2", "40", "2"}}

But the problem is every i call the function. it will always adding same data, i would like to just do refresh or replace maybe, when i do some event. my code

line_data = {}

function ReadData()
    local file = io.open("data.txt", "r")
    for line in file:lines() do
        line_data[#line_data+1] = { line:match('([^:]+):(%d+):(%d+)') }
    end
end

maybe you guys can help me with this situation ?

4
  • This can't be answered generally. How do you get the data? How do you know if a line is new? Two simple cases i can spontaneously think about: A new line is simply appended to the file or you can distinguish the lines by their beginning ("header"): test1, test2, testN. Is this the case? Commented Nov 10, 2016 at 6:56
  • oops my bad, this line_data = {}. actually have null at the beginning. i just declare it to array for being a saved array with contain value whenever i try call function read Commented Nov 10, 2016 at 7:40
  • this line on loop `line_data[#line_data+1] = { line:match('([^:]+):(%d+):(%d+)') }. it will make an array with contains value that already split by line:match . Commented Nov 10, 2016 at 7:42
  • Yes, that is clear, and it does not change anything. Every time you call ReadData the old data get appended as well. So as i said, you need to clarify in which cases new data appear. Tell us a bit more about your problem, show a minimum working example. Show us complete code, with all removed but the essential lines. It is now unclear when and how often and in which context you call ReadData. Commented Nov 10, 2016 at 7:45

2 Answers 2

1

If I've right understand, you'll overwrite the line_data after re-reading the file. So I think the best way is to get the array from the reading function itself and replace the old one. Here my example:

function ReadData(_path)
    local tmp = {}
    local file = io.open(_path, "r")
    for line in file:lines() do
        tmp[#tmp+1] = { line:match('([^:]+):(%d+):(%d+)') }
    end
    file:close()
    return tmp
end

function Array2dAsStr(_array)
    local function cutRight(_s, _i)
        _i = _i or 1
        return _s:sub(1, (_s:len())-1*_i)
    end
    local sOut = '{'
    for _, v in pairs(_array) do
        sOut = sOut..'{'
        for _, v1 in pairs(v) do
            sOut = sOut..v1..','
        end
        sOut = cutRight(sOut)..'},'
    end
    return cutRight(sOut)..'}'
end

line_data = ReadData("data.txt")
print(Array2dAsStr(line_data))

-- if you read again the file, the old stuff from line_data will overwritten
line_data = ReadData("data.txt")
print(Array2dAsStr(line_data))
Sign up to request clarification or add additional context in comments.

1 Comment

this is what i mean. yes, it's working well. thanks!
0

If you want to fill existing lines with new data, then you need some id. If that id is just the name, i.e. first part of string, then change line where you append data to array:

-- old code    
-- line_data[#line_data+1] = { line:match('([^:]+):(%d+):(%d+)') }

-- new code
local name, score1, score2 = line:match('([^:]+):(%d+):(%d+)')
line_data[name] = {score1, score2}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.