Jump to content

  • Curse Sites
Help

Table Stacking Lua Code


  • Please log in to reply
12 replies to this topic

#1 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 12 April 2013 - 02:17 PM

Hi i want to track some SpellIDs with special IDs

my spellID table
SpellLibrary = {8122,113792, ...}

and for check
for k, v in ipairs(SpellLibrary) do
if spellID == SpellLibrary[k] then
print(SpellLibrary[k])
end
end
this works like it shoud but...

...i want to change my table for categorys like this
SpellLibrary = {Category1 = {8122, 113792}, Category2 = {xxx, xxx}}

i thought the check part should look like:
for k, v in ipairs(SpellLibrary[Category1]) do
if spellID == SpellLibrary[Category1[k]] then
print(SpellLibrary[Category1[k]])
end
end
..but it doesnt work..

#2 Lag123

Lag123
  • Premium Junkies
  • Curse Premium
  • Humanclass_name
  • EU-Ysera
  • Reckoning / Abrechnung
  • Posts: 3
  • Talents: Assassination 1/1/1/2/0/1

Posted 12 April 2013 - 04:50 PM

It's maybe better when you make it like this


The table

SpellLibrary = {
["Category1"] = {
[1234] = true,
[5678] = true,
}
["Category2"] = {
[1111] = true,
[2222] = true,
}
}

-- Check Single category
print(SpellLibrary["Category1"][spellID])
-- Check all categorys
for k,v in pairs(SpellLibrary) do
if v[spellID] then
print(v[spellID])
end
end


#3 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 12 April 2013 - 05:46 PM

View PostLag123, on 12 April 2013 - 04:50 PM, said:

It's maybe better when you make it like this


The table

SpellLibrary = {
["Category1"] = {
[1234] = true,
[5678] = true,
}
["Category2"] = {
[1111] = true,
[2222] = true,
}
}

-- Check Single category
print(SpellLibrary["Category1"][spellID])
-- Check all categorys
for k,v in pairs(SpellLibrary) do
if v[spellID] then
print(v[spellID])
end
end
doesnt work either :/

#4 Lag123

Lag123
  • Premium Junkies
  • Curse Premium
  • Humanclass_name
  • EU-Ysera
  • Reckoning / Abrechnung
  • Posts: 3
  • Talents: Assassination 1/1/1/2/0/1

Posted 12 April 2013 - 05:59 PM

Forgot comma in table
SpellLibrary = {
["Category1"] = {
[1234] = true,
[5678] = true,
},
["Category2"] = {
[1111] = true,
[2222] = true,
}
}
-- Check Single category
print(SpellLibrary["Category1"][spellID])
-- Check all categorys
for k,v in pairs(SpellLibrary) do
if v[spellID] then
print(v[spellID])
end
end


#5 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 12 April 2013 - 06:27 PM

View PostLag123, on 12 April 2013 - 05:59 PM, said:

Forgot comma in table
SpellLibrary = {
["Category1"] = {
[1234] = true,
[5678] = true,
},
["Category2"] = {
[1111] = true,
[2222] = true,
}
}
-- Check Single category
print(SpellLibrary["Category1"][spellID])
-- Check all categorys
for k,v in pairs(SpellLibrary) do
if v[spellID] then
print(v[spellID])
end
end
result is nil not one of the id, tho

#6 Vadrak

Vadrak
  • Premium
  • Curse Premium
  • Posts: 21

Posted 12 April 2013 - 08:05 PM

SpellLibrary = {
["Category1"] =
{
8122,
113792,
},
};
for key, value in ipairs(SpellLibrary["Category1"]) do
if ( spellID == value ) then
print(value);
end
end

Something like this would be my guess. On the other hand, depending on what the categories are for, you also could remove the for-loop completely. In ArenaLive (I think LoseControl does it the same way) I use this method to track the CC-Category to which a spell belongs:
ARENALIVE_SPELLS = {
["portraitOverlay"] = {
-- Druid
[33786] = "defCD", -- Cyclone
[102795] = "stun", -- Bear Hug
[5211] = "stun", -- Mighty Bash
[9005] = "stun", -- Pounce (stun)
},
};

if ( ARENALIVE_SPELLS["portraitOverlay"][spellID] ) then
local spellsCategory = ARENALIVE_SPELLS["portraitOverlay"][spellID];
print("spellID "..spellID.." belongs to category "..spellsCategory);
end

This way you get both, spellID and your category in an easy manner, because the spellID must be a valid table-key to turn the if-query true and the spellCategory is the table-entrie's value.

Quote

why would u ever cast holy fire instead of mindspike?

Quote

i like shooting thunderbolts from the sky at my enemies just like my pikachu
Twitter: @VadrakAL

#7 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 12 April 2013 - 11:01 PM

View PostVadrak, on 12 April 2013 - 08:05 PM, said:

SpellLibrary = {
["Category1"] =
{
8122,
113792,
},
};
for key, value in ipairs(SpellLibrary["Category1"]) do
if ( spellID == value ) then
print(value);
end
end

Something like this would be my guess. On the other hand, depending on what the categories are for, you also could remove the for-loop completely. In ArenaLive (I think LoseControl does it the same way) I use this method to track the CC-Category to which a spell belongs:
ARENALIVE_SPELLS = {
["portraitOverlay"] = {
-- Druid
[33786] = "defCD", -- Cyclone
[102795] = "stun", -- Bear Hug
[5211] = "stun", -- Mighty Bash
[9005] = "stun", -- Pounce (stun)
},
};

if ( ARENALIVE_SPELLS["portraitOverlay"][spellID] ) then
local spellsCategory = ARENALIVE_SPELLS["portraitOverlay"][spellID];
print("spellID "..spellID.." belongs to category "..spellsCategory);
end

This way you get both, spellID and your category in an easy manner, because the spellID must be a valid table-key to turn the if-query true and the spellCategory is the table-entrie's value.
or i could create a table for each category without a Major table .. i will have a look later

#8 DU5KNOIR

DU5KNOIR
  • Junkies
  • Posts: 748
  • LocationRegina, Saskatchewan

Posted 12 April 2013 - 11:17 PM

Don't iterate over the table to find the spell - that's probably the slowest way you could do it!  What you want is a hash table.  Vadrak's suggestion is the best and most efficient, and used by dozens upon dozens of well-made addons.  The goal is to provide the ID and get the category or nil back with no step in between.
is he still a fly guy clappin' if nobody ain't hear it

#9 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 16 April 2013 - 02:12 PM

thanks so far...

another question:
is there an easier way to get the number of the categorys?!

for now i count each key
local count = 0; for _ in pairs(SpellLibrary) do count = count + 1 end print(count)


#10 Vadrak

Vadrak
  • Premium
  • Curse Premium
  • Posts: 21

Posted 16 April 2013 - 04:28 PM

View PostSyncrow, on 16 April 2013 - 02:12 PM, said:

thanks so far...

another question:
is there an easier way to get the number of the categorys?!

for now i count each key
local count = 0; for _ in pairs(SpellLibrary) do count = count + 1 end print(count)

Depends if  the categories are named [1], [2], [3] or ["category1"], ["category2"], ["category3"]. For tables ordered by indexes you should use the #-operator ( #(tableName) ), otherwise I think your solution is the only possible way to do it. If you have a static amount of categories that you can foresee at the beginning of the code, I'd suggest you to deposit the number in a global. Maybe at the beginning of your SpellLibrary's file. This way you could change the number, if it is necessary from update to update and you wouldn't have to iterate over your table all the time. But if the user is able to change the amount of categories and if they are named in string-format then your solution is the way to go I guess.

Quote

why would u ever cast holy fire instead of mindspike?

Quote

i like shooting thunderbolts from the sky at my enemies just like my pikachu
Twitter: @VadrakAL

#11 Eveny

Eveny
  • Junkies
  • Night Elfclass_name
  • EU-Outland
  • Misery
  • Posts: 245
  • Talents: Shadow 1/0/0/1/2/1
  • LocationBelgium

Posted 16 April 2013 - 04:38 PM

Quote

-- Create a 3 x 5 array
grid = {}
for i = 1, 3 do
grid[i] = {}

for j = 1, 5 do
grid[i][j] = 0 -- Fill the values here
end
end

maybe something like this could work if you make a 2dimensional array and use 2 for loops

Edited by Eveny, 16 April 2013 - 04:38 PM.

If you have problems understanding my post, don't assume something and hit the reply button but read it again.

#12 Syncrow

Syncrow
  • Premium
  • Curse Premium
  • Posts: 183

Posted 16 April 2013 - 05:55 PM

thanks for the quick response!

#13 Resike

Resike
  • Premium Junkies
  • Curse Premium
  • Humanclass_name
  • EU-Outland
  • Misery
  • Posts: 76
  • Talents: Arms 2/1/0/1/1/0
  • RBG: 2294

Posted 26 April 2013 - 12:19 AM

SpellLibrary = {Category1 = {8122, 113792}, Category2 = {xxx, xxx}}
for k, v in ipairs(SpellLibrary) do
for i = 1, table.getn(k) do
if spellID == k[i] then
print(k[i])
end
end
end





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

<