Lua find a key from a value
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm working with this:
chars = {
["Nigo Astran"]="1",
["pantera"]="2"
}
nchar = (chars[$name])+1
The variable $name will give me a string that I'm logged in to, in this case : "Nigo Astran"
and nchar has the value "2" if I'm in "Nigo Astran", and so on. I believe you get the idea.
Now, I want to get the key from the value, example:
when nchar is 2 it should give me "pantera" as the key. I'm just not getting the value of the key.
lua
add a comment |
I'm working with this:
chars = {
["Nigo Astran"]="1",
["pantera"]="2"
}
nchar = (chars[$name])+1
The variable $name will give me a string that I'm logged in to, in this case : "Nigo Astran"
and nchar has the value "2" if I'm in "Nigo Astran", and so on. I believe you get the idea.
Now, I want to get the key from the value, example:
when nchar is 2 it should give me "pantera" as the key. I'm just not getting the value of the key.
lua
add a comment |
I'm working with this:
chars = {
["Nigo Astran"]="1",
["pantera"]="2"
}
nchar = (chars[$name])+1
The variable $name will give me a string that I'm logged in to, in this case : "Nigo Astran"
and nchar has the value "2" if I'm in "Nigo Astran", and so on. I believe you get the idea.
Now, I want to get the key from the value, example:
when nchar is 2 it should give me "pantera" as the key. I'm just not getting the value of the key.
lua
I'm working with this:
chars = {
["Nigo Astran"]="1",
["pantera"]="2"
}
nchar = (chars[$name])+1
The variable $name will give me a string that I'm logged in to, in this case : "Nigo Astran"
and nchar has the value "2" if I'm in "Nigo Astran", and so on. I believe you get the idea.
Now, I want to get the key from the value, example:
when nchar is 2 it should give me "pantera" as the key. I'm just not getting the value of the key.
lua
lua
edited Jul 13 '15 at 13:05
ryanpattison
5,31011428
5,31011428
asked Oct 28 '11 at 4:14
WeskerWesker
143129
143129
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
If you find yourself needing to get the key from the value of a table, consider inverting the table as in
function table_invert(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
add a comment |
I don't think there is anything more efficient than looping over the entries in the table using pairs and comparing the keys.
you can do that using something like this
function get_key_for_value( t, value )
for k,v in pairs(t) do
if v==value then return k end
end
return nil
end
Then you'd use it like this:
local k = get_key_for_value( chars, "1" )
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
add a comment |
the best way to do that is like this
local autoreply={
['hey']='hi',
['how are u']='am fine what about u?',
['how r u']='am fine what about u?',
['how are you']='am fine what about u?',
['sleep']='rockabye bayby good dreems',
['السلام']='وعليكم السلام ورحمة الله وبركاته',
}
local keys={'hey','how are u','how r u','how are you','sleep','السلام'}
function getValueFromKey(table,key)
for k,v in ipairs(keys)do
if string.find(string.upper(key),string.upper(v)) then return table[v] end
end
return false
end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7925090%2flua-find-a-key-from-a-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you find yourself needing to get the key from the value of a table, consider inverting the table as in
function table_invert(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
add a comment |
If you find yourself needing to get the key from the value of a table, consider inverting the table as in
function table_invert(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
add a comment |
If you find yourself needing to get the key from the value of a table, consider inverting the table as in
function table_invert(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
If you find yourself needing to get the key from the value of a table, consider inverting the table as in
function table_invert(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
edited Aug 31 '13 at 19:09
answered Oct 28 '11 at 10:08
lhflhf
56.7k668106
56.7k668106
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
add a comment |
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
well atm im using this code function get_key_for_value( t, value ) for k,v in pairs(t) do if v==value then return k end return nil end now im finding another problem, when my table ends, and returns a nil i want it, to loop from the first value, cant do that yet <,<
– Wesker
Oct 28 '11 at 16:09
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
The for loop is missing a do.
– HeMan
Aug 31 '13 at 19:04
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
@HeMan, fixed, thanks.
– lhf
Aug 31 '13 at 19:09
add a comment |
I don't think there is anything more efficient than looping over the entries in the table using pairs and comparing the keys.
you can do that using something like this
function get_key_for_value( t, value )
for k,v in pairs(t) do
if v==value then return k end
end
return nil
end
Then you'd use it like this:
local k = get_key_for_value( chars, "1" )
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
add a comment |
I don't think there is anything more efficient than looping over the entries in the table using pairs and comparing the keys.
you can do that using something like this
function get_key_for_value( t, value )
for k,v in pairs(t) do
if v==value then return k end
end
return nil
end
Then you'd use it like this:
local k = get_key_for_value( chars, "1" )
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
add a comment |
I don't think there is anything more efficient than looping over the entries in the table using pairs and comparing the keys.
you can do that using something like this
function get_key_for_value( t, value )
for k,v in pairs(t) do
if v==value then return k end
end
return nil
end
Then you'd use it like this:
local k = get_key_for_value( chars, "1" )
I don't think there is anything more efficient than looping over the entries in the table using pairs and comparing the keys.
you can do that using something like this
function get_key_for_value( t, value )
for k,v in pairs(t) do
if v==value then return k end
end
return nil
end
Then you'd use it like this:
local k = get_key_for_value( chars, "1" )
edited Jan 4 at 4:37
Dale Burrell
3,47952655
3,47952655
answered Oct 28 '11 at 4:20
Michael AndersonMichael Anderson
46.7k699150
46.7k699150
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
add a comment |
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
ok that works but sadly, i cant do like <pre> nchar = (chars[$name])+1 <code> get_key_for_value( chars, nchar ) will be nil idk why, that was my problem at the start xD
– Wesker
Oct 28 '11 at 4:39
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
got it i did it with tostring() love you anyways <3
– Wesker
Oct 28 '11 at 4:58
add a comment |
the best way to do that is like this
local autoreply={
['hey']='hi',
['how are u']='am fine what about u?',
['how r u']='am fine what about u?',
['how are you']='am fine what about u?',
['sleep']='rockabye bayby good dreems',
['السلام']='وعليكم السلام ورحمة الله وبركاته',
}
local keys={'hey','how are u','how r u','how are you','sleep','السلام'}
function getValueFromKey(table,key)
for k,v in ipairs(keys)do
if string.find(string.upper(key),string.upper(v)) then return table[v] end
end
return false
end
add a comment |
the best way to do that is like this
local autoreply={
['hey']='hi',
['how are u']='am fine what about u?',
['how r u']='am fine what about u?',
['how are you']='am fine what about u?',
['sleep']='rockabye bayby good dreems',
['السلام']='وعليكم السلام ورحمة الله وبركاته',
}
local keys={'hey','how are u','how r u','how are you','sleep','السلام'}
function getValueFromKey(table,key)
for k,v in ipairs(keys)do
if string.find(string.upper(key),string.upper(v)) then return table[v] end
end
return false
end
add a comment |
the best way to do that is like this
local autoreply={
['hey']='hi',
['how are u']='am fine what about u?',
['how r u']='am fine what about u?',
['how are you']='am fine what about u?',
['sleep']='rockabye bayby good dreems',
['السلام']='وعليكم السلام ورحمة الله وبركاته',
}
local keys={'hey','how are u','how r u','how are you','sleep','السلام'}
function getValueFromKey(table,key)
for k,v in ipairs(keys)do
if string.find(string.upper(key),string.upper(v)) then return table[v] end
end
return false
end
the best way to do that is like this
local autoreply={
['hey']='hi',
['how are u']='am fine what about u?',
['how r u']='am fine what about u?',
['how are you']='am fine what about u?',
['sleep']='rockabye bayby good dreems',
['السلام']='وعليكم السلام ورحمة الله وبركاته',
}
local keys={'hey','how are u','how r u','how are you','sleep','السلام'}
function getValueFromKey(table,key)
for k,v in ipairs(keys)do
if string.find(string.upper(key),string.upper(v)) then return table[v] end
end
return false
end
answered Dec 18 '17 at 10:08
MasterMaster
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7925090%2flua-find-a-key-from-a-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown