mIRC /hswap

By Arigateaux on Feb 09, 2017

From a recommendation by Jamie on LunarIRC.net, I created /hswap. It's a little weird how I have it set up for a part of it, but the rest works like it should. Feedback is encouraged, since I don't know if I want to keep the optional swap info. It basically works one of two ways:

First, you can do an even swap, item for item, which just changes the values between the tables.
Second, you can swap two items completely, removing each item from the table it's being sent from.

; /hswap <from_table> <to_table> <item> [<item>]
alias hswap {
  var %table_from = $$1
  var %table_to = $$2
  var %item_from = $$3
  var %item_to = $4

  if (!$hget(%table_from)) echo -ag %table_from must exist in order to do that.
  else if (!$hget(%table_to)) echo -ag %table_to must exist in order to do that.
  else if (!$hget(%table_from, %item_from)) echo -ag %item_from must exist in order to do that.

  var %swap_item_from = $hget(%table_from, %item_from)

  if (%item_to) {
    if (!$hget(%table_to, %item_to)) echo -ag %item_to must exist in order to do that.
    else {
      var %swap_item_to = $hget(%table_to, %item_to)
      hadd %table_to %item_from %swap_item_from
      hadd %table_from %item_to %swap_item_to
      if (%item_to != %item_from) {
        hdel %table_to %item_to
        hdel %table_from %item_from
      }
    }
  }
  else { 
    var %swap_item_to = $hget(%table_to, %item_from)
    hadd %table_to %item_from %swap_item_from
    hadd %table_from %item_from %swap_item_to
  }
}

Comments

Sign in to comment.
gooshie   -  Feb 13, 2017

I like the way it's written except I'd test that
the item ends up where it should instead of:

if (%item_to != %item_from) {
        hdel %table_to %item_to
        hdel %table_from %item_from
}

I'd also want a verbose option; something along the lines of:
Foo was moved from TableFoo to TableBar which now reads: Yada Yada Yada...
Bar was moved from TableBar to TableFoo which now reads: Blah Blah Blah...
(I'd truncate the actual data to a reasonable length to keep it in one line.)

Arigateaux  -  Feb 13, 2017

So the idea behind %item_to != %item_from is to solve a problem with /hswap test_a test_b val1 val1 working the same as /hswap test_a test_b val1

I will also add a verbose mode with the -s switch that is used for the other hash functions.

gooshie  -  Feb 13, 2017

Well, it's up to you, but your conditional test
before deletion is an ambiguous pseudo test.
I wouldn't consider it good coding practice.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.