mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2007
Posts: 1,129
T
Tomao Offline OP
Hoopy frood
OP Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
I would like to pick your guys' brains about calculating the same hash table data into its total number.

Currently I have these in my hash table data:
Quote:
3 apples,9 apples,8 strawberries,7 grapes,2 watermelons
As you can see there are two identical data values for 3 apples and 9 apples. How do I add them up into one data as 12 apples, preferably make them appear like this in the hash table data:
Quote:
12 apples,8 strawberries,7 grapes,2 watermelons
Thanks in advance.

There will be more repeated data values being stored, and I need the same ones to be added together as one data with its total calculation.

Last edited by Tomao; 23/05/11 06:08 PM.
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Code:
alias consolidatefruit {
  tokenize 44 $1
  var %i = 1, %result
  while ($eval($ $+ %i,2) != $null) {
    set -l $+(%,$gettok($v1,2,32)) $calc($eval(% $+ $gettok($v1,2,32),2) + $gettok($v1,1,32))
    inc %i
  }
  %i = 1
  while ($var(%*,%i) != $null) {
    if ($var(%*,%i).local) && (!$istok(% $+ result % $+ i,$var(%*,%i),32)) { %result = %result $+ , $+ $var(%*,%i).value $mid($var(%*,%i),2) }
    inc %i
  }
  return $mid(%result,2)
}


$consolidatefruit(<string>)

Example:

Code:
alias test {
  var %fruit = 3 apples,9 apples,8 strawberries,5 apples,6 grapefruit,5 grapefruit,3 bananas,8 apples,150 bananas,6 oranges,3 limes,1000000 bananas,-1 oranges
  echo -a $consolidatefruit(%fruit)
}


Output:

25 apples,8 strawberries,11 grapefruit,1000153 bananas,5 oranges,3 limes

There might be a better/faster way to do it but this one works smile

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
You would probably be better off doing this outside of the table and then adding in the final result to the table. There are a variety of ways to do what you want, depending on where the data is coming from and how it's being stored in the hash table.

To give you the best code possible, you'd need to give more details about what's being done here.

Example details you could provide (you can provide others):

* Does the table item always have the same fruits (or whatever)? For example, will it always have apples and grapes and so on? This is helpful because it's easier to write a short regex if you know the item will be listed.

* Are the items always the same ones (no one can add something new)? For example, it doesn't take random words from people. This is useful because you could very easily make the fruit/etc order be the same all of the time and then just use $gettok() with $calc() or similar.

* Does the table need everything listed in one item, or can they have their own items? This is useful because if each fruit/etc can have its own item in the table, then it's extremely easy to update the numbers.

* Any other info would help. Where are you getting the input from (users, generated from script, etc)? What's the purpose of the script? Etc. We could give you something that would work without knowing the information, but it would be almost guaranteed to be inefficient for what you actually need.


EDIT: hixxy beat my post and did give you a working method. I still think that you can get a better method if you provide the additional details.

Last edited by Riamus2; 23/05/11 06:41 PM.

Invision Support
#Invision on irc.irchighway.net
Joined: Jul 2007
Posts: 1,129
T
Tomao Offline OP
Hoopy frood
OP Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
That is very nice of you, hixxy. By the look of it, I believe it'll work a charm once I give it a whirl. Many thanks to you.

Riamus2, this is what I do to add the hash table data:
Code:
hadd -m fruits $nick $addtok($hget(fruits,$nick),$r(3,15) $read(fruits.txt),44)
Then someone uses a command to trigger the code below to find out how many fruits he or she has picked:
Code:
msg # $iif($hfind(fruits,$$2),$v1 $+ 's Fruits: $replace($hget(pp,$2),$chr(44),$+($chr(44),$chr(32))),$2 is not a fruit picker.)
I hope this gives you a better, if not too thorough, idea as to what I've done.

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Use $consolidatefruit() at the point of adding and it should be more efficient than if you were to do it every time you read from the table.

Tbh it should be fine for singular adds/reads like this, it would maybe need a bit of speeding up if you were planning to loop through every item in the table passing them all to the alias.

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
An alternate method, though you may or may not be interested in using it, would be to have the table items set up like: nick.apples, nick.bananas, etc. and only have the number for each. You can even have an "index" item (nick.index) that lists all the types of fruit the person has (using $addtok()). The benefit of that is that you can very easily list the total of any given fruit rather than trying to find the right token each time. Of course, you may not ever need an individual fruit's total.

Btw, using $addtok() the way you have it currently is "dangerous". If you already have something like 3 apples and then you $addtok() 3 apples again, it would not create a new token but would skip adding it because the token is already there. hixxy's script or a similar script that sums before adding solves that issue, but I'm just mentioning it so that you know.


Invision Support
#Invision on irc.irchighway.net

Link Copied to Clipboard