• For In Loop Help!
    5 replies, posted
So lets take this table for example. [lua] local Market = {} Market[1] = "Fish" Market[2] = "Vegetables" Market[3] = "Grains" Market[4] = "Fruits" Market[5] = "Dairy" [/lua] Now if I want to see if the market has more (Fish, Vegetables, and Grains) then (Fruits and Dairy) how would I go about doing that.
Firstly, lay out your tables like this : [lua] local Market = {}; Market["Fish"] = true; Market["Vegetables"] = true; Market["Grains"] = true; Market[ "Fruits"] = true; Market[ "Dairy"] = true; [/lua] This way you can check if the market contains something without looping through the table : [lua] if ( Market["Vegetabes"] ) then // Yup, vegetables end [/lua]
[QUOTE=>>oubliette<<;39819368]Firstly, lay out your tables like this : [lua] local Market = {}; Market["Fish"] = true; Market["Vegetables"] = true; Market["Grains"] = true; Market[ "Fruits"] = true; Market[ "Dairy"] = true; [/lua] This way you can check if the market contains something without looping through the table : [lua] if ( Market["Vegetabes"] ) then // Yup, vegetables end [/lua][/QUOTE] I appreciate your help, but I should have been more specific. In the case that the market changes, say it might not always have an index of 5 and the values might be different how would I go about looping through it. -Edit- In other words...say someone opens up a 2nd fish market.
[lua] for key, value in pairs( Market ) do // Loops through all the keys and values end [/lua] .. If you actually want to implement a system where a market changes and you want to test if it has a item in it, you shouldn't loop it, do what I said above.
[QUOTE=>>oubliette<<;39819506][lua] for key, value in pairs( Market ) do // Loops through all the keys and values end [/lua] .. If you actually want to implement a system where a market changes and you want to test if it has a item in it, you shouldn't loop it, do what I said above.[/QUOTE] Okay, and for my last question how would I check if the total index of a collection of values in the market is greater than another collection? Thanks a lot by the way.
Provided that both tables are indexed by numbers, you can do : [lua] if ( #table1 > #table2) then // blah blah blah end [/lua] Otherwise you will need to [lua] if ( table.Count(table1) > table.Count(table2) ) then // blah blah blah end [/lua]
Sorry, you need to Log In to post a reply to this thread.