• How to get an exact value in a table ?
    12 replies, posted
I have some problem with table and I need to get an exact value from my table who have this inside: [code] tomato = red apple = green onion = white[/code] in example I have the Table named "MyTable" and I sort the value with this [lua] for i = 1, k do for k,v in pairs(MyTable) do print ( v ) end end [/lua] Any one have an idea why I have just the value "white" printed 3 times and not all those 3 values ; red,green,white ? Thanks
[CODE] local MyTable = {tomato = "red", apple = "green", onion = "white"} for k,v in pairs(MyTable)do print(v) end [/CODE] or you could just use PrintTable(MyTable)
Oh thanks it working :) I got another question, how can I add the value of my table in a Dmenu ? I have this but i'm not sure how to add it and all the value need to be on separate line and can be pasted on the DTextEntry. [lua] local TextEntryBar = vgui.Create("DTextEntry", PanelBottomGrnd) TextEntryBar:SetText( "" ) TextEntryBar:SetPos( 10, 10 ) TextEntryBar:SetSize( 230, 19 ) -- TextEntryBar:GetValue() .. "\n" for i = 1, k do TextEntryBar.OnTextChanged = function ( btn ) local MenuButtonOptions = DermaMenu() -- Creates the menu MenuButtonOptions:AddOption("hello", function() Msg("Hello") end ) -- Add options to the menu MenuButtonOptions:Open() -- Open the menu AFTER adding your options end[/lua]
[lua] local MyTable = {tomato = "red", apple = "green", onion = "white"} local TextEntryBar = vgui.Create("DTextEntry", PanelBottomGrnd) TextEntryBar:SetText( "" ) TextEntryBar:SetPos( 10, 10 ) TextEntryBar:SetSize( 230, 19 ) TextEntryBar.OnTextChanged = function ( btn ) local MenuButtonOptions = DermaMenu() -- Creates the menu MenuButtonOptions:AddOption("hello", function() Msg("Hello") end ) -- Add options to the menu for k,v in pairs(MyTable) do MenuButtonOptions:AddOption(k,function() print(v) end) end MenuButtonOptions:Open() -- Open the menu AFTER adding your options end[/lua] And stop with the for i=1,k do.
[lua] local MyTable = { } MyTable[1] = blaah = "Red", yeaa = "Green", pwnage = "White" MyTable[2] = blaah = "White", yeaa = "Red", pwnage = "Green" MyTable[3] = blaah = "Red", yeaa = "White", pwnage = "Green" for k,v in pairs(MyTable)do print(v.yeaa) -- does print the tables "yeaa" value print(v.pwnage) -- does print the tables "pwnage" value print(v.blaah) -- does print the tables "blaah" value end [/lua]
thanks for all idea :) I have an interrogation about this again, if I got [code]MyTestTable = { "White","Red","Yellow","Green"}[/code] How I can print the only the value "Red" in the console ? or to be more exact , how to print the second value or the third one ?
[lua]print(MyTestTable[2])[/lua] Prints Red in the console. Hey, how about you stop coming here for every trivial problem like this and take some time to read this instead: [url]http://www.lua.org/pil/[/url]
I already read all of this but there is some things there is not the same like the gmod wiki.
[QUOTE=lotus006;26543123]I already read all of this but there is some things there is not the same like the gmod wiki.[/QUOTE] You didn't read it. [url]http://www.lua.org/pil/2.5.html[/url]
oh sorry :( But i'm sure this is not in pil [url]http://www.facepunch.com/threads/1035136-How-to-remove-this-small-part-in-the-DPanelList[/url]
How would I remove a specific value from my table, without knowing what its position is? table.remove doesn't do that.
[lua]function table.RemoveByValue(tbl , value) for k , v in pairs(tbl) do if v == value then tbl[k] = nil end end return tbl end [/lua]
And if you're working with a classic table indexed by number, this won't leave nasty nil holes in your table after the process: [lua]function table.RemoveByValue(tbl, value) for i=#tbl,1,-1 do if tbl[i] == value then table.remove(tbl, i) end end end[/lua]
Sorry, you need to Log In to post a reply to this thread.