Table Iteration
Good Morning,
I was curious about how I may go about ensuring a table of mine is iterated over in a certain order (simply, top to bottom.) I tried to use the ipairs() function for my loop, and it didn't work; and using pairs(), it reads my table in a rather erratic fashion. My table utilizes the key and value as information, so I can't use the key as an index. The next() function seems to produce the same results as pairs(). Thanks! |
The order in which key/value pairs are returned by pairs is undefined, so, basically random. next works the same way. The only way to directly walk through a table in a specific order is to use ipairs.
There are some workarounds, but they all involve creating an extra table with only the keys from the original table as indexed values, sorting it somehow, and then using ipairs on the extra table and value lookups on the original table, eg: Code:
local t = { Code:
local t = { Without knowing what you're actually doing, it's hard to say what the best method will be. |
Hey Phanx,
Thanks for the reply. What I am trying to accomplish is to have two tables of relevant buffs and debuffs, and their respective modification to healing. An example would be: Lua Code:
The way healing modifications are handled is multiplicative, from highest to lowest value. What I aim to do is sort the table from highest to lowest by hand, and by having my loop iterate over it in descending order, I can ensure the appropriate buff is calculated first. That being said, I need to be able to modify the values through a look-up of the keys (to reflect glyphs); and the table would then need to be re-sorted to reflect the new values. I am not sure that I like the idea of two tables. Is this a situation where metatables would be useful? I was poking around some Lua websites and there was a method utilizing metatables to emulate the next() function, but I wasn't too sure it was what I was looking for. Thanks for your time and help (as usual :rolleyes:). |
If your code is not performance critical, it may be easier to have a table of tables instead, something like:
lua Code:
|
Quote:
Also why does the order matter if it's multiplicative? |
Quote:
I suppose you are correct about the two table approach, but the metatable could be universal for two seperate tables, yielding only 3 tables, instead of four? (Bare in mind I know nothing about metatables, I just saw it used as a solution to a problem similar to mine). Quote:
My approach was, and this is pretty theoretical, to sort a table of healing increases (and decreases in a separate table) by hand, and as a for-loop iterates over it (assuming it is in descending order), if a player has a buff in the table, that percentage will be applied to an amount, and the process continues. In the example posted above, if a player had BOTH Guardian Spirit and Vampiric Blood, a potential heal would be increased by 60%, then 25(or 40)%. My approach could be horrid, but it seemed like the most simple approach, until I ran into the issue with my table being impossible to index, and impossible to iterate over with ipairs(). Thanks for the replies! Edit: Here was the script I was using (granted, it doesn't work in that it reads the table in any order, but it still works) Lua Code:
|
Quote:
lua Code:
Quote:
|
Quote:
|
I suppose you could run tests to see how much it actually affects performance. Coming up with some other work-around possibly involving metatables might still create a similar performance hit though, I'm not sure.
I'll let someone more experienced in the performance area comment on that :P |
I just want to reiterate that the order does not actually matter when you're multiplying like this.
Try it yourself. |
Quote:
Thanks for the help guys, but it should work. |
<-- math teacher
As F16Gaming pointed out in the last line of his example, the Associative Property of Multiplication - just like the Associative Property of Addition - proves that order does not matter when dealing with more than 2 numbers. (a*b)*c = a*(b*c) which also equals (a*c)*b. (When dealing with just 2 numbers, you are looking at the Commutative Property -> a*b = b*a.) Hope this simplifies your process! :) /edit: bah - you replied to this concept as I was typing... :p |
I don't even want to talk about it :mad:
Thanks so much for the help. *Sobs in corner* |
As a side issue, I am wondering why you are doing this. I wrote Simply Magical Healer ages ago and recorded all the modifications (buffs and debuffs) that affected healing. It was actually quite complicated back then since Blizzard seemed to do a variety of things. That addon lost its major use when Blizzard removed the ability to automatically target spell casts, and then when it removed spell ranks. I have no idea what Blizzard does with healing modifications now, but was wondering what your use would be for this information.
|
Hello Nihmfree,
In a nut-shell, I have a known amount of healing incoming, and I need to know what it will be after healing modifications (increases and decreases). Rather than run over all of the player's buffs, I'd rather run over a list of buffs that I know the player will have. Edited for grammar; not my day :( |
I'd go with something like this:
Code:
local healBuffs = { |
For someone who often discourages people from sending stuff in PMs/giving out email... :D
Why don't you just put it up somewhere public and share with the world? :P I'm sure there are others who'd find a spreadsheet with that kind of info useful. |
First I had the same thought. But then I realized that would just create a place with old outdated informations we will have to fight later on ;)
|
1 Attachment(s)
I'm against people using PMs/email for getting answers to their questions, because we have an entire forum system set up for that. I'm not against using PMs/email for sharing files, because forum software isn't very good at being a file repository. Also, this particular forum doesn't allow ODS attachments. :p
But, here you go anyway. Remove the TXT extension. |
Well there's Dropbox and all those other file sharing services that allow you to hotlink/share special links for your public files. (Not like those crappy file hosting websites with 30 second wait times, eww...)
|
All times are GMT -6. The time now is 03:50 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI