0

i really need help. i am still can't figure out my logic lua code about How can i block the event with an if condition, when it already has a value?

this is my array2d :

mydata = {
{"mike", "30", "1"},
{"michael", "40", "2"},
{"mike", "40", "2"},
{"michael", "50", "3"},
{"frost", "50", "3"},
{"nick", "60", "4"}
}

actually, my plan is to get the correct code to check the index name mydata[..][1] and point index mydata[..][3].

This is an example case explanation :


so if the index name Michael and point index 3 are not exist or if only name michael exist but it doesn't have point index 3, then it will write michael:50:3 in the data.txt. but if name and point index already exist. Then, it will block and print("sorry your name and score index already exist")

My Code :

mydata = {{"mike", "30", "1"},
            {"michael", "40", "2"},
            {"mike", "40", "2"},
            {"michael", "50", "3"},
            {"frost", "50", "3"},
            {"nick", "60", "4"}}

function check_point1(tab, val1) -- Function for checking name
    for index, value in ipairs (tab) do
        if value[1] == val1 then
            return true
        end
    end
    return false
end

function check_point2(tab, val1) -- Function for checking player point after name
        for index, value in ipairs (tab) do
            if value[3] == val1 then
                return true
            end
        end
        return false
    end

-- this is example case
-- these below are variable to process checking if it's exist
name = "michael"
id = "3"

-- this is my checking code, idk it's good or not. or maybe it's wrong code. because it's not working
if check_point1(mydata, name) then
  if check_point2(mydata, id) then
    print(your name and point index are exist. sorry you can't earn this point")
  else
    print(only your name exist. let us save your new point now")
  end
else
  print("your name and point index is not exist. let us save your name and point now")
  local file = io.open("data.txt", "r")
  file:write("michael:50:3")
  file:close()
end

What would be the best way for blocking an event "write michael:50:3" when it's already exists ? let me know if there some thing you need to understand before you help me out. i am still poor at lua.

3
  • 1
    Han, you've asked a few similar questions now, and they all seem to revolve around basic program flow, simple algorithms & the standard Lua library. Asking questions is good - it helps you to learn - but you might also find you learn well by taking a more generalized approach to your studies. Make sure to read Programming in Lua, keep the Reference Manual handy, and consider sitting in on a local college course, or taking an online intro to programming course, as some general programming lessons would go a long way. Commented Nov 12, 2016 at 13:46
  • yeah i know, i asking alot of times. i should try learn more by myself, but idk. i already tried so many times and try thinking about how lua works, but lua is really hard. so i ask more question not because i want finish some script i try. but this can be my lesson. i actually, when i first time ask question and someone answered with how it works and reason my code not working. it just helped me alot.. with my learning. i am not just learning lua only, but i am learning most of programming languages like c#, java, and etc. and i want learn lua too, but i just need help. because its different Commented Nov 12, 2016 at 15:50
  • than other language. Commented Nov 12, 2016 at 15:51

1 Answer 1

1

We can break this down into a generalized comparison function, one which searches a set of records for a stored record which matches a provided one (partial or full).

The first function we'd have to define would be a helper function, contains_all, which takes a target record, and a query record, and tests to see if the target contains at least everything the query does.

local function contains_all (target, query)
    for key, value in pairs(query) do
        if target[key] ~= value then
            return false
        end
    end

    return true
end

We can then easily implement our search function, which searches through a set of records, and checks each record against the same query record. We call this function find_by because in the event that it finds a matching record, we return that record, and the index it was found at.

local function find_by (set, query)
    for i = 1, #set do
        if contains_all(set[i], query) then
            return set[i], i
        end
    end
end

Now we can simply use these to find out if mydata contains a record which matches or partially matches our query record of choice.

local mydata = {
    {"mike", "30", "1" },
    { "michael", "40", "2" },
    { "mike", "40", "2" },
    { "michael", "50", "3" },
    { "frost", "50", "3" },
    { "nick", "60", "4" }
}

print(find_by(mydata, { [1] = "michael", [3] = "3" })) --> table: 0x217d890 4

Since our stored records are sequences, we use explicit bracket notation to set our indices, skipping the second index in this case.

Since this function returns a table in the event of a match, and nil in the event of no match, we now have a safe boolean constrast; tables are a truthy value, and nil is a falsy one.

if find_by(mydata, { [1] = "michael", [3] = "3" }) then
    print('Entry exists')
end

Returning the record makes it easier for us to make further checks, without querying every record again.

local found = find_by(mydata, { "michael" })

if found then
    if found[3] == "3" then
        print('Entry exists.')
    else
        print('Only name exists.')
    end
else
    print('Entry does not exist')
end
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your explanation, Oka. You really helped me. it's working fine. i would glad to learn more about these functions something that i havent discovered method.

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.