I'll try to explain the logical steps of the process, pointing to the parts of the script that implement each step.
The first step is to group the identical words together. For example, if the $readini() returns something like this:
"beer fanta cola beer beer fanta cola fanta beer"
we want to reorder the words so that the line becomes:
"beer beer beer beer cola cola fanta fanta fanta"
Why? Because we are then able to break apart the line into chunks that only contain a number of identical words. The chunks in our example are
beer beer beer beer
fanta fanta fanta
So, we break the line apart and then count how many times each word is repeated in each chunk.
The sorting of the line read from barman.ini is done with $sorttok($readini($scriptdirbarman.ini,bill,$nick),32)
The division of the line into chunks of identical words is done essiantially by 'tokenizing' the string, ie inserting characters (token separators) between different chunks. I chose the LF character ($lf or $chr(10)) for that, because it is safe: it can never be inside a word. The insertion of the LF separators is done with the $regsub()
. I'm afraid I can't really say anything else about this part, since regular expressions knowledge is required; and those who do know regex will probably figure out this part anyway.
After the $regsub call, our string looks like this:
"beer beer beer beer<LF>cola cola<LF>fanta fanta fanta<LF>"
The next step is to split this line into tokens. This can be done in two ways: $gettok() or /tokenize. I chose tokenize, because it allows me to use the $* identifiers, thus avoiding the loop. So we perform a /tokenize on the line stored in %barman:tokenize 10 %barman
Then we call the alias named 'barman
' and pass it $*
: this way, mirc executes the barman alias $0 times, each time passing it $1, $2, $3 etc. But the /tokenize command has filled $1 with "beer beer beer beer", $2 with "cola cola" and $3 with "fanta fanta fanta". Now, the barman alias, that sees each token as $1-, can easily get the number of instances of the word (which is $1) in $1-, which is $0. So it sets the variable like :
%barman = <old %barman value>, <total number of instances of a word> <word>
This is done for each LF-separated token in the original string, as we said.
%barman has now become
", 4 beer, 2 cola, 3 fanta"$mid(%barman,3)
is the same as $right(%barman,-2) and just cuts off the first two characters from %barman, (the trailing comma and the space right after it).
I guess it turned out to be a bit longer than a few in-code comments :tongue: IMPORTANT
I just noticed that I forgot to unset %barman before I call /barman. So one more "unset %barman" is required, just above "barman $*". This way, the /barman alias will start with a clean %barman variable.
!.echo -q $regsub($sorttok($1-,32),/(\S+)((?: \1)*)(?: |$)/g,\1\2 $+ $lf,%barman)
tokenize 10 %barman
msg # $nick $+ 's bill: $mid(%barman,3)
alias barman %barman = %barman $+ , $0 $1