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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Removing a table entry using a wildcard from an alias.

Removing a table entry using a wildcard from an alias.

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


Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Wed 20 Jul 2016 03:15 PM (UTC)

Amended on Wed 20 Jul 2016 03:18 PM (UTC) by Wuggly

Message
Hello,
I created very simple friends and enemies tables.

To add friends, I made an alias "add friend *"

To remove them, it's "rem friend *"

and the it goes the same way for enemies.

Problem is, I can't seem to figure out how to remove them from the table.

Here's the function with the last way I tried..

function who_remF (name, line, wildcards, styles)
 friendIS = wildcards[1]
 who_friends.friendIS = nil
 print(friendIS .. " has been removed from Friends List")
 who_win () -- redraws who list window with correct colors
end -- function who_remF


Assuming it's trying to find a key called friendIS to nil.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Wed 20 Jul 2016 09:44 PM (UTC)
Message
That should be:


 who_friends [friendIS] = nil


What you had would remove the friend called "friendIS" from the table, not what that variable held.

- Nick Gammon

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

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #2 on Wed 20 Jul 2016 11:08 PM (UTC)

Amended on Thu 21 Jul 2016 04:10 AM (UTC) by Wuggly

Message
That's how I tried it the first time and didn't have any luck, except I didn't have a space in between who_friends and [friendIS].
So I tried it again with the space this time, and still no luck.


Function used:
--------------
function who_remF (name, line, wildcards, styles)
 friendIS = wildcards[1]
 who_friends [friendIS] = nil
 print(friendIS .. " has been removed from Friends List")
 who_win () -- redraws who list window with correct colors
end -- function who_remF

LOG OF TESTS:
-------------
who test

1="Blake"
2="Chayston"

rem friend Blake

Blake has been removed from Friends List

who test

1="Blake"
2="Chayston"

rem friend 1

1 has been removed from Friends List

who test

1="Blake"
2="Chayston"

add friend Nick

Nick has been added to Friends List

who test

1="Blake"
2="Chayston"
3="Nick"

rem friend Nick

Nick has been removed from Friends List

who test

1="Blake"
2="Chayston"
3="Nick"

rem friend "Nick"

"Nick" has been removed from Friends List

who test

1="Blake"
2="Chayston"
3="Nick"


I do serialize it in between sessions, perhaps that could be messing it up? The "who test" command you see me sending is a simple tprint of the table and it seems to look okay after loading back up.


In the OnPluginSaveState () function..

 SetVariable ("who_friends", serialize.save ("who_friends"))
 SetVariable ("who_enemy", serialize.save ("who_enemy"))

and in the OnPluginInstall () function..

 require "serialize"
 who_friends = {}
 assert (loadstring (GetVariable ("who_friends") or "")) ()
 who_enemy = {}
 assert (loadstring (GetVariable ("who_enemy") or "")) ()


EDIT: so I've spoken to some people who gave me some tips, but haven't tried it yet.


First tip I got was...
 for pos, val in ipairs(t) do if val == friendIS then table.remove(t, pos) break end end

Then another person jumped in and said...
in the future, consider using a map, where you insert with t[friendIS] = true 
and remove with t[friendIS] = nil, and iterate with pairs() instead of ipairs() 
(but the iteration is unordered)


I'm thinking of trying the second one. Just posting this so you know if I'm on the right track or not heh.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Thu 21 Jul 2016 05:23 AM (UTC)
Message
Oh, I assumed you were using a map, because of the way you removed the friend. You see you get better advice if you post all your code an not just a couple of lines.

S/he is right, you could add a friend as suggested, and remove in the way I described. A map won't necessarily be in order but you can use pairsByKeys to print them in order if that is what you want, eg.



  require "pairsbykeys"
  
  for k, v in pairsByKeys (who_friends) do
    print (k)
  end -- for





Example code:


require "pairsbykeys"

who_friends = { }

function addFriend (who)
  who_friends [who] = true
end -- addFriend 

function removeFriend (who)
  who_friends [who] = nil
end -- removeFriend 

function showFriends ()
  
  for k, v in pairsByKeys (who_friends) do
    print (k)
  end -- for
end -- showFriends 

-- test

print (string.rep ("-", 40))

addFriend ("Nick")
addFriend ("Blake")
addFriend ("Chayston")

showFriends  ()

removeFriend ("Blake")

print (string.rep ("-", 40))

showFriends  ()


Output from above:


----------------------------------------
Blake
Chayston
Nick
----------------------------------------
Chayston
Nick

- Nick Gammon

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

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Thu 21 Jul 2016 05:24 AM (UTC)
Message
Instead of just "true" for the value of the friend entry you could have other stuff, like what class they are, or an entire table (eg. time added, class, race, etc.)

- Nick Gammon

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

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #5 on Thu 21 Jul 2016 01:00 PM (UTC)
Message
Thanks for the examples and tips.

The order doesn't really bother me. In the who list, when it's building/updating, I have it check the friends & enemies list then it colors them the appropriate color.

I made the default colors dodgerblue for friends, and red for enemies, but like everything else I've built so far, those colors have the option of being changed to something else by the user.

Perhaps I could make it its own plugin as well and add it to the plugin forum if that doesn't exist on there yet, although it would be specifically for servers that shows the who list like that, with race, class, name, then title.

Or maybe I could explore around other servers, and get a general idea of the different ways it is shown, and have it let you choose what your who list looks like, and run accordingly.

With all the help I've been getting here since first starting, I just feel the need to contribute something, since I can't afford donating at the moment, although that is on my big list of things to do once I'm settled financially again. =)

Anyways, thank you.
[Go to top] top

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #6 on Thu 21 Jul 2016 04:35 PM (UTC)

Amended on Thu 21 Jul 2016 04:41 PM (UTC) by Wuggly

Message
So...

I tried to get a bit more fancy when it comes to detecting the keys if they match the current who table (since it was a bit of a mess before, but worked), but I can't get it working..

When the who list is displayed/updated I tried doing the checks to see if they are enemy/friend using a function.

So I could simply do "if has_key(who_friends, whoNa) then"
and "if has_key(who_enemy, whoNa) then"

whoNa is just the character's name when it runs through the who list table (whoT)

Here's the function currently..

function has_key (wList, wName)
 for k, v in pairsByKeys (wList) do
  if k == wName then
   print("did it work?")
   return true
  end -- k
 end -- for
 return false
end -- has_key
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Thu 21 Jul 2016 09:44 PM (UTC)

Amended on Fri 22 Jul 2016 04:48 AM (UTC) by Nick Gammon

Message
Once you have a keyed table, you don't need to iterate at all.


function has_key (wList, wName)
  return wList [wName] ~= nil
end -- has_key


That's the whole point of tables with keys. You directly test them.

Eg. to test if Nick is in the table:


if who_friends ["Nick"] then
  print "Nick is a friend"
end -- if


Mind you, this is *case sensitive*. If you want case-insensitive, force the key to lower case when you put it in the table. Then you might want to keep the original. eg.


function addFriend (who)
  who_friends [string.lower (who)] = who
end -- addFriend 


Now the key will be lower-case (so to check for someone being there you force to lower-case). The value is now the real name (with capitalization).

So, has_key becomes:


function has_key (wList, wName)
  return wList [string.lower (wName)] ~= nil
end -- has_key

- Nick Gammon

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

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #8 on Thu 21 Jul 2016 11:49 PM (UTC)

Amended on Fri 22 Jul 2016 12:02 AM (UTC) by Wuggly

Message
I tried

whoNa = string.match(whoT, "( %a+)$")
if who_friends[whoNa] then

and

if has_key(who_friends, whoNa) then

neither seemed to work.

also changed to what you had except with ~= since != errors out



Here's the whole thing if that helps.

  if inv ~= nil then
   local y = 0
   for i, styles in ipairs (inv) do
     local x = 5
     for _, style in ipairs (styles) do
	  whoT = string.match (style.text, "^(.- %] %a+)")
	  if whoT == nil then
	   whoT = string.match(style.text, ".*")
	   if whoT == string.match(whoT, "^(.*Total.*)") then
	    x = x + WindowText (who_wind, "fontfour", whoT, 25, y + 1, 0, 0, colourGold)
	   end -- whoT double check
	  else
	   whoRa = string.match(whoT, "^(%a+ .-)")
	   whoCl = string.match(whoT, "^.-(%[.*%])")
	   whoNa = string.match(whoT, "( %a+)$")
	   if who_friends[whoNa] then
	    x = x + WindowText (who_wind, "fontfour", whoNa, x, y + 15, 0, 0, ColourNameToRGB("dodgerblue"))
	   elseif has_key(who_enemy, whoNa) then
	    x = x + WindowText (who_wind, "fontfour", whoNa, x, y + 15, 0, 0, ColourNameToRGB("red"))
	   else
	    x = x + WindowText (who_wind, "fontfour", whoNa, x, y + 15, 0, 0, style.textcolour)
	   end -- who_friends
	   if whoCl == nil then
            -- if class is a colored name, it doesnt capture it, figure some fix for it here
	   else
	    WindowText (who_wind, "fontfour", whoCl, 80, y + 15, 0, 0, style.textcolour)
	   end -- whoCl
	  end -- whoT
     end -- for
     y = y + font_height
   end -- for each who list item
  else
   WindowText (who_wind, "fontfour", "Who List will show up here", 25, 1, 0, 0, colourGold)
  end -- inv
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Fri 22 Jul 2016 04:50 AM (UTC)
Message
Quote:

also changed to what you had except with ~= since != errors out


Whoops! I'm doing too much C++ programming. I fixed the post.




Can you please post a small piece of code (test using the Intermediate window) that demonstrates your point?

Posting a line, and saying "it doesn't work" doesn't prove much. A lot depends on the variable contents, the data types, how you know it didn't work, etc.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Fri 22 Jul 2016 09:38 PM (UTC)

Amended on Fri 22 Jul 2016 09:39 PM (UTC) by Nick Gammon

Message
A possible issue is this:


	   whoNa = string.match(whoT, "( %a+)$")


Note that the match (whoNa) will have a space in it. (You have the space inside the brackets).

So now you might have "Nick" as the table key, and attempting to match on " Nick" is a totally different thing - one is four bytes and one is five bytes, for a start.

- Nick Gammon

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

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #11 on Tue 26 Jul 2016 11:34 AM (UTC)

Amended on Thu 28 Jul 2016 01:40 AM (UTC) by Wuggly

Message
b
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Fri 29 Jul 2016 06:57 AM (UTC)
Message
b?

- 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.


24,418 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 HostDash]