Thread Tools Display Modes
02-15-12, 08:33 PM   #1
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Repeating a variable

lua Code:
  1. RDXDB.SetPackageMetadata(TRUESYS.PKG[1], TRUESYS.MD[1], true);
  2. RDXDB.SetPackageMetadata(TRUESYS.PKG[1], TRUESYS.MD[2], true);
  3. RDXDB.SetPackageMetadata(TRUESYS.PKG[1], TRUESYS.MD[3], true);

I have to repeat:

RDXDB.SetPackageMetadata(TRUESYS.PKG[1-3], TRUESYS.MD[1-9], true);

and I figure there must be a shortcut instead of just writing it all out 27 different times. <_< Any advice?

Or would I be better off just writing it out, It seems to me that the short cut would help with bloat? o.o
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **
  Reply With Quote
02-15-12, 08:44 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,953
wouldn't a simple for loop work ?

Code:
for i = 1,3 do
   1.RDXDB.SetPackageMetadata(TRUESYS.PKG[1], TRUESYS.MD[i], true);
end
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
02-15-12, 09:13 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
To expand on (and complete) what Xrystal suggested...

lua Code:
  1. for i=1,3 do
  2.      for g=1,9 do
  3.           RDXDB.SetPackageMetadata(TRUESYS.PKG[i], TRUESYS.MD[g], true)
  4.      end
  5. end

Documentation: http://www.lua.org/pil/4.3.4.html
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
02-15-12, 09:14 PM   #4
unlimit
Lookin' Good
 
unlimit's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 484
Thanks Xrystal, thanks Seerah!

It's definitely simple, but I never really understood what the "do" operator was, and never imagined what it did was make a loop, which is awesome to know for the future too!
__________________


kúdan: im playing pantheon
JRCapablanca: no youre not
** Pantheon has been Banned. **
  Reply With Quote
02-16-12, 07:00 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
"do" on its own does not create a loop. This, for example, is not a loop:

Code:
do
    local n = 42
    function PrintN()
        print("The number is:", n)
    end
end
"do ... end" creates a scoping block. In the above example, the variable "n" is only available inside the "do ... end" block, but the function "PrintN" is available anywhere, since it isn't local to the block.

A loop simply defines a "do ... end" block, and adds some code that causes the block to be repeated.

Code:
for i = 1, 3 do
    print(i * 3)
end
... is simply a shortcut for doing:

Code:
local i = 1
print(i * 3)
i = i + 1
print(i * 3)
i = i + 1
print(i * 3)
Of course, you could also simplify that to:
Code:
print(1 * 3)
print(2 * 3)
print(3 * 3)
But, depending on the complexity of the task you need to repeat with an incrementing value, a loop rapidly becomes far more efficient to write and maintain.

Also, loops make it much easier to run a block of code X times, where X is not a number of you know beforehand and can change depending on other factors. For example, if you wanted to print the names of everyone in your guild, you could do:

Code:
local name = GetGuildRosterInfo(1)
if name then print(name) end

name = GetGuildRosterInfo(2)
if name then print(name) end

name = GetGuildRosterInfo(3)
if name then print(name) end

name = GetGuildRosterInfo(4)
if name then print(name) end
... and so on and so forth all the way up to 1000 (the current maximum size of a guild). Obviously this would be extremely tedious to write, even worse to modify (if you wanted to change what happened for each guild member, for example), and take up a lot of space. A loop can make that much less painful:

Code:
for i = 1, GetNumGuildMembers() do
    local name = GetGuildRosterInfo(i)
    print(name)
end
Here, the upper limit on the loop is dynamic, and depends on how many characters are actually in your guild when you run the code.

We can also use the "break" operator to stop running a loop before it reaches the maximum index. This is useful for things like getting information about buffs on the player, since there is no function that tells you how many buffs are on you.

Code:
local total = 0
for i = 1, 40 do
    local name = UnitBuff("player", i)
    if name then
        print("Buff #" .. i .. ": " .. name)
        total = total + 1
    else
        break
    end
end
print("Total buffs: " .. total)
We use 40 as the upper limit on the loop, because that's the maximum number of buffs you can have, but we also use the "break" operator so that after we've gotten through all of the buffs, we don't keep running the loop. If you only have 3 buffs, for example, the loop will only run 4 times (once for each buff, then once more to determine that there aren't any more buffs) instead of 40 times.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Repeating a variable


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off