Data structures 如何在Lua中创建HashMap的等价物

Data structures 如何在Lua中创建HashMap的等价物,data-structures,dictionary,lua,redis,hashmap,Data Structures,Dictionary,Lua,Redis,Hashmap,我希望在lua中有一个类似Java HashMap的简单数据结构 这样做的目的是,我希望维护一个唯一的键“userID”,映射到一组不断更新的两个值,例如 '77777', {254, 24992} 关于如何实现这一点,有什么建议吗 只需使用一个由userID和值索引的Lua表,另一个Lua表有两个条目: T['77777']={254, 24992} 只需使用一个由userID和值索引的Lua表,另一个Lua表有两个条目: T['77777']={254, 24992} 假设您正在询问的

我希望在lua中有一个类似Java HashMap的简单数据结构

这样做的目的是,我希望维护一个唯一的键“userID”,映射到一组不断更新的两个值,例如

'77777', {254, 24992}
关于如何实现这一点,有什么建议吗


只需使用一个由userID和值索引的Lua表,另一个Lua表有两个条目:

T['77777']={254, 24992}

只需使用一个由userID和值索引的Lua表,另一个Lua表有两个条目:

T['77777']={254, 24992}

假设您正在询问的是该词典:

local dictionary = {
    ['77777'] = {254, 24992},
    ['88888'] = {253, 24991},
    ['99999'] = {252, 24990},
}

棘手的是,键是一个无法转换为Lua变量名的字符串,因此必须用[]围绕每个键。我在中找不到关于此规则的明确描述,但中说,如果一个键由下划线、字母和数字组成,但不以数字开头,则在以上述方式定义时不需要[],否则需要方括号。

假设字典是您要问的:

local dictionary = {
    ['77777'] = {254, 24992},
    ['88888'] = {253, 24991},
    ['99999'] = {252, 24990},
}

棘手的是,键是一个无法转换为Lua变量名的字符串,因此必须用[]围绕每个键。我在中找不到对此规则的明确描述,但中说,如果一个键由下划线、字母和数字组成,但不以数字开头,则在以上述方式定义时不需要[],否则需要方括号。

这是解决方案的可能实现

local usersTable = {}

function topUsers(key, count, size)
    if usersTable[key] then
        usersTable[key][1] = usersTable[key][1] + count
        usersTable[key][2] = usersTable[key][2] + size
    else
        usersTable[key] = {count, size}
    end
end

function printTable(t)
    for key,value in pairs(t) do 
        print(key, value[1], value[2])
    end
end

这是解决方案的可能实现

local usersTable = {}

function topUsers(key, count, size)
    if usersTable[key] then
        usersTable[key][1] = usersTable[key][1] + count
        usersTable[key][2] = usersTable[key][2] + size
    else
        usersTable[key] = {count, size}
    end
end

function printTable(t)
    for key,value in pairs(t) do 
        print(key, value[1], value[2])
    end
end

在{LUA=localdictionary={+['77777']={254,24992},+['8888']={253,24991},+['9999']={252,24990}+}+返回字典中尝试了这个私有静态最终字符串READ\u SCRIPT;对象o=jedis.evalString.formatREAD\u SCRIPT\u IN\u LUA;System.out.printlno.toString;输出:[]我做错了什么?在[u LUA=local dictionary={+['77777']={254,24992},+['888']={253,24991},+['9999']={252,24990}+返回dictionary中尝试了这个私有静态最终字符串READ_SCRIPT_;对象o=jedis.evalString.formatREAD\u SCRIPT\u IN\u LUA;System.out.printlno.toString;输出:[]我做错什么了吗?