[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Combine two tables including a multiple count
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Combine two tables including a multiple count

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


Posted by Blainer   (191 posts)  [Biography] 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
[Go to top] top

Posted by Bast   (77 posts)  [Biography] 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
[Go to top] top

Posted by Blainer   (191 posts)  [Biography] bio
Date Reply #2 on Wed 01 Jul 2009 02:50 AM (UTC)
Message
Thanks
[Go to top] 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.


4,670 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]