table help

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

table help

Post by Caled »

Code: Select all

powers={}
powers.maul={wyvern, icewyrm, jaguar}
powers.sting={wyvern}
powers.incinerate={wyvern}
powers.shred={wyvern, bear}
powers.trumpet={elephant}
powers.stampede={elephant}
powers.yank={elephant}
powers.reflexes={icewyrm, jaguar, wolverine}
powers.resistance={jaguar, basilisk}
powers.freeze={icewyrm}
powers.pound={gorilla}
powers.hoist={wyvern, eagle}
powers.fly={wyvern, eagle, condor, owl, jackdaw}
powers.negate={basilisk}
powers.gaze={basilisk}
powers.glare={basilisk}
powers.tailslap={wyvern, swordfish}
powers.claw={hyena, wolverine}
powers.howl={hyena}
powers.rest={sloth}
powers.ambush={jaguar}
powers.blast={elephant}
powers.might={icewyrm, wyvern}
1. I'm not sure if that syntax is actually properly creating the table.
2. I don't know how to display a table like that, so I have no way of using trial-and-error to test if I have the table right.

Could someone please help me write a function for #2 (#1 I can test and then work out myself if only I have #2 working.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: table help

Post by Heiko »

Code: Select all

powers={}
powers.maul={"wyvern", "icewyrm", "jaguar"}
(...)

Code: Select all

powers={}
powers.maul={"wyvern", "icewyrm", "jaguar"}
if type(powers.maul) == 'table' then printTable( powers.maul ) end
For more table print functions have a look at luaGlobal.lua in mudlet/src.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: table help

Post by Caled »

A new question about tables, this time when returning a value.

Code: Select all

function morphCheck(p)
	local x=0
	for k,v in pairs(powers[p]) do
		if morph == v then x=1 end
	end
	if x == 0 then doMorph(powers.p.1) end    -- this line does not work
end
if x == 0 then doMorph(powers[p].1) end -- this line also does not work

How do I return the first value of the table

Edit:
if x == 0 then doMorph(powers[p[1]]) end

^The above at least compiles correctly, so I am getting closer - but it returns a nil value rather than the value matching the 1st key.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: table help

Post by Heiko »

You have to give the complete script next time, otherwise help is more guess work than anything as I know nothing about the MUD you play.

Code: Select all

function caled()
    powers={}
    powers.maul={"wyvern", "icewyrm", "jaguar"}
    if type(powers.maul) == 'table' then printTable( powers.maul ) end
	
    morph = "jaguar";
    morphCheck("maul")
end

function morphCheck(p)
   local x=0
   for k,v in ipairs(powers[p]) do
        echo( k .. " -> " .. v .. "\n" );
        if morph == v then x=1 end
   end
   if x == 1 then doMorph(powers[p][1]) end
end

function doMorph( what )
	echo("morphing:<" .. what .. ">\n" );
end

caled();
Hope this is what you were after.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: table help

Post by Caled »

Thanks:

powers[key][key]

is exactly what I was after. I didn't paste the whole script because I didn't need help with the whole script, but I apologise if that made anything more difficult for you.

Post Reply