Search code examples
arraysmultidimensional-arraylualua-tableluafilesystem

lua array contains specific value for checking further


sorry if i am bothering you now, i am still learning. but i need help. could you correct me and script how to check and then get value of array 2d for checking further and counting points ?

example array2d syntax that i build :

role = {{[name],[points],[indexpoint]},
        {[...],[...],[...]}}

example array2d value that i make :

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

what i wanted is. when i am searching for name "michael". it will detect value in the array. something like this

local player_data = {{"michael", 40, "2"},{"michael", 50, "3"}}

so after this, i can count the points that he already have. 40+50 and the result "90" will send to new variable like resultpoint = 90

so the print will show like this

Player "Michael"
Your points is "90"
Here is the list of your index that you earned :
1. earn 40 points in index point "2"
2. earn 50 points in index point "3"

my long code here :

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

function check_role1(tab, val)
        for index, value in ipairs (tab) do
            -- We grab the first index of our sub-table instead for player name
            if value[1] == val then
                return true
            end
        end
        return false
    end

    function check_role2(tab, val)
        for index, value in ipairs (tab) do
            -- We grab the third index of our sub-table instead for index point
            if value[3] == val then
                return true
            end
        end
        return false
    end

    function detectroles(name)
        pn = name
        if check_role1 (role, pn) then
            print ('Yep')
            --[[for i = 1, #role do
                 player_checkname[i] = role[i][1] -- Get Player Name From Array for checking further
                 player_checkpnt[i] = role[i][2] -- Get Player Point From Array for checking further
                 player_checkidpnt[i] = role[i][3] -- Get Player Point From Array for checking further]]
             -- is this correct code to get value ?
            end
        else
            print ('You dont earn any points')
        end
    end

    detectroles("jack") -- this is call function, for checking name jack if he is in array or not

is this really possible ? if there is a simple way or more less code, let me know. i know, it's too much code. i am still newbie


Solution

  • What you seem to be looking for are some general data structure functions known as filter (sometimes called select) and reduce.


    filter is a simple function which operates on a set of values, creating a new set containing only those which conform to a provided predicate. The implementation of filter is very straight-forward:

    • Create a new, empty set
    • Iterate through your existing set, reading each value
    • Push any values which pass the test into the new set

    The result of the operation is the new set.

    In Lua:

    local function filter (list, test)
        local result = {}
    
        for index, value in ipairs(list) do
            if test(value, index) then
                result[#result + 1] = value
            end
        end
    
        return result
    end
    

    We can use this function to get a filtered set of values, where the first entry in each table is 'michael':

    local set = {
        { "mike", "30", "1" },
        { "michael", "40", "2" },
        { "mike", "40", "2" },
        { "michael", "50", "3" },
        { "frost", "50", "3" },
        { "nick", "60", "4" }
    }
    
    local filtered_set = filter(set, function (person)
        return person[1] == 'michael'
    end)
    
    for _, person in ipairs(filtered_set) do
        print(unpack(person))
    end
    
    --[[stdout:
      michael   40  2   
      michael   50  3
    ]]
    

    reduce is a function which accumulates a single value by iterating upon a set of values. reduce typically allows for a provided initial value, otherwise the initial value is the first value in the set.

    In Lua:

    local function reduce (set, action, initial_value)
        local result
        local index
    
        if initial_value ~= nil then
            result = initial_value
            index = 1
        else
            result = set[1]
            index = 2
        end
    
        for i = index, #set do
            result = action(result, set[i], i)
        end
    
        return result
    end
    

    Which we can use to determine a combined value for set entries:

    local value = reduce(filtered_set, function (score, next_entry)
        return score + next_entry[2] -- Careful with relying on stringly-math
    end, 0)
    
    print(value) --> prints 90
    

    Though absent from the Lua Standard Library, these are very common functional set operations, and learning how to implement them (and others like each, map, reject, count, index, has, find) will teach you a lot about working with data structures.

    Try thinking about how they would fit into your current code.