Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Combine two tables including a multiple count

Combine two tables including a multiple count

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Blainer   (191 posts)  Bio
Date Tue 30 Jun 2009 05:45 PM (UTC)
Message
I can't for the life of me figure out how to do this.

I'm building an inventory table from the wildcards table returned from a trigger. I want to insert the wildcards table into the inventory table only if it's unique and add a quantity field at position 0, if it's not I want to add 1 to the quantity field.

This is what I have so far:

wildcards[0] = nil
if #invTcache == 0 then
	table.insert (invTcache, wildcards)
	invTcache[1][0] = 1
else
	t = invTcache
	for i,v in pairs (t) do
		if v[3] == wildcards[3] then
			invTcache[i][0] = invTcache[i][0] + 1
		else
			table.insert (invTcache, wildcards)
			invTcache[i][0] = 1
		end
	end
end


Wildcard table:
1="159820476"
2=""
3="a @Wsugar @Ycoated @Cdonut@w"
4="0"
5="14"
6="0"
7="-1"
8="-1"
1="157482523"
2=""
3="a water flask"
4="0"
5="12"
6="0"
7="-1"
8="-1"

What I want to finish with:
1:
1="159820476"
2=""
3="a @Wsugar @Ycoated @Cdonut@w"
4="0"
5="14"
6="0"
7="-1"
8="-1"
0="1" <<--- item count
2:
1="157482523"
2=""
3="a water flask"
4="0"
5="12"
6="0"
7="-1"
8="-1"
0="2" <<--- item count

Thank you
Top

Posted by Bast   (78 posts)  Bio
Date Reply #1 on Tue 30 Jun 2009 06:14 PM (UTC)

Amended on Tue 30 Jun 2009 06:17 PM (UTC) by Bast

Message
Try something like this:


wildcards[0] = nil
if #invTcache == 0 then
    table.insert (invTcache, wildcards)
    invTcache[1][0] = 1
else
    t = invTcache
    found = false
    -- search for match, change found to true if we find one
    for k,v in pairs (t) do
      if v[3] == wildcards[3] then
        found = true
        invTcache[k][0] = invTcache[k][0] + 1
      end
    end
    if not found then
      wildcards[0] = 1
      table.insert (invTcache, wildcards)   
    end
end

Bast

Scripts: http://github.com/endavis
Top

Posted by Blainer   (191 posts)  Bio
Date Reply #2 on Wed 01 Jul 2009 02:50 AM (UTC)
Message
Thanks
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,578 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.