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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Question regarding tables

Question regarding tables

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


Pages: 1  2 

Posted by Edoren   (31 posts)  [Biography] bio
Date Reply #15 on Sun 22 Apr 2007 05:03 AM (UTC)

Amended on Sun 22 Apr 2007 05:05 AM (UTC) by Edoren

Message
oh duh, i didn't even think about using the names for that! hmm, does the same thing work for inserting then? ie -

table.insert (lastfish.fish, (wildcards.fish))
table.insert (lastfish.pounds, wildcards.pounds)
table.insert (lastfish.ounces, (wildcards.ounces) or 0)?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #16 on Sun 22 Apr 2007 06:07 AM (UTC)
Message
I'm not exactly sure what you mean by "inserting" in this case. If you do this:


lastfish.fish = wildcards.fish


You *have* inserted wildcard.fish into the lastfish table, keyed by the name "fish".

When you use table.insert you are inserting something with a numeric key, which you probably don't want here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #17 on Sun 22 Apr 2007 06:16 AM (UTC)
Message
To illustrate that, consider this test:


require "tprint"

lastfish = {}

lastfish.fish = "cod"
lastfish.pounds = 42
lastfish.ounces = 10

tprint (lastfish)


Output

"fish"="cod"
"ounces"=10
"pounds"=42


Here we have put details about the fish into the table, keyed by the item name (eg. lastfish.fish is "cod").

Next time the trigger fires, it will replace the items with the new ones.

If you use table.insert, things just get a numeric key, like this:


require "tprint"

lastfish = {}

table.insert (lastfish, "cod")
table.insert (lastfish, 42)
table.insert (lastfish, 10)

tprint (lastfish)

Output

1="cod"
2=42
3=10


You can see that isn't as easy to use. Also, if you insert more stuff, it gets added, not replaced:


table.insert (lastfish, "flounder")
table.insert (lastfish, 16)
table.insert (lastfish, 12)

tprint (lastfish)

Output

1="cod"
2=42
3=10
4="flounder"
5=16
6=12



Try reading my post about Lua tables:

http://www.gammon.com.au/forum/?id=6036

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Edoren   (31 posts)  [Biography] bio
Date Reply #18 on Sun 22 Apr 2007 06:28 AM (UTC)
Message
well the thing was i didn't want to replace something based on name since, the fish names are basically only a few different types. so if i wanted to keep a running tally/list of what i had caught it would be replaced rather than being stored everytime it finds the same name.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #19 on Sun 22 Apr 2007 06:49 AM (UTC)
Message
You want to keep tally of each individual fish, is that it? You still don't want table.insert. Something like this is more like it:


lastfish [wildcards.fish] = {
  pounds = wildcards.pounds, 
  ounces = wildcards.ounces }


What that will do is create a sub-table, in the lastfish table, keyed by the name of the fish, and inside it will be the details for that type.

eg.

The table might contain:


lastfish.cod.pounds = 42
lastfish.cod.ounces = 10
lastfish.flounder.pounds = 11
lastfish.flounder.lounces = 2



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Edoren   (31 posts)  [Biography] bio
Date Reply #20 on Sun 22 Apr 2007 11:54 AM (UTC)
Message
aha! that's what i was looking for.
[Go to top] top

Posted by Edoren   (31 posts)  [Biography] bio
Date Reply #21 on Sun 22 Apr 2007 06:28 PM (UTC)

Amended on Sun 22 Apr 2007 08:14 PM (UTC) by Edoren

Message
hmm what's wrong with this? numkey wont ever add.

function reel_stop (name, line, wildcards)
 local numkey=numkey or 0
 local gold=gold or 0
 local pounds=tonumber (wildcards.pounds) or 0
 local ounces=tonumber (wildcards.ounces) or 0
  EnableTrigger ("reel_caught", false)
  Send ("put fish in kitbag")
  lastfish [numkey] = {
   fish = wildcards.fish,
   pounds = wildcards.pounds, 
   ounces = wildcards.ounces }
  if pounds > 0 then
   count= pounds*16 + ounces
   if gold > 0 then
    gold=gold + count*0.8
   else
    gold=count*0.8
   end --gold
  else
   if gold > 0 then
    gold=gold + ounces*0.8
   else
    gold=ounces*0.8
   end --gold
  end --pounds
  numkey=numkey+1
 end --stop reel
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #22 on Sun 22 Apr 2007 08:42 PM (UTC)
Message
You made numkey local, so each time the function is called, it is back to nil. Remove the word "local" from in front of it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


47,121 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

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 HostDash]