Line 1: | Line 1: | ||
− | The '''$*''' allows you to iterate over all of the tokens contained within $1-. The way this works is much like a while loop. $* has been omitted from the help file since while loops were introduced | + | The '''$*''' allows you to iterate over all of the tokens contained within $1-. The way this works is much like a while loop. $* has been omitted from the help file since while loops were introduced. |
$* is extremely powerful, because it is much faster than using a while loop on a list of tokens. Understanding how this works is pretty simple, so consider the following: | $* is extremely powerful, because it is much faster than using a while loop on a list of tokens. Understanding how this works is pretty simple, so consider the following: | ||
Line 17: | Line 17: | ||
Quirky? Quirky, how? | Quirky? Quirky, how? | ||
− | Well, mIRC takes the command $* | + | Well, mIRC takes the command $* appears in, and replace all of the occurrences in the line by a special marker: `~$*: You can see that by doing for example: |
<syntaxhighlight lang="mIRC">//tokenize 32 abcd | echo -a $left($*,1) $+ $chr(3) $+ $mid($*,2)</syntaxhighlight> | <syntaxhighlight lang="mIRC">//tokenize 32 abcd | echo -a $left($*,1) $+ $chr(3) $+ $mid($*,2)</syntaxhighlight> | ||
− | Let's take a look at this: | + | Let's take a look at this and understand why the above return such a value: |
<syntaxhighlight lang="mIRC">//tokenize 32 abcd | echo -a $mid($*,2)</syntaxhighlight> | <syntaxhighlight lang="mIRC">//tokenize 32 abcd | echo -a $mid($*,2)</syntaxhighlight> | ||
Why isn't this returning bcd? Because of the usage of the special marker `~$*, mIRC has stored that the command is actually "echo -a $mid(`~$*,2)". | Why isn't this returning bcd? Because of the usage of the special marker `~$*, mIRC has stored that the command is actually "echo -a $mid(`~$*,2)". | ||
− | + | Then, for each token, (here only $1 == abcd), mIRC evaluates the line, here $mid(`~$*,2) becomes ~$*, and then mIRC replaces the marker by the token and executes the echo command. However, after an operation, like $mid here, that marker cannot be found; | |
+ | |||
+ | Basically, it cannot be guaranteed to get the correct value of $* inside an identifier. | ||
There is a workaround for the above issue, and that workaround is by using scid and scon: | There is a workaround for the above issue, and that workaround is by using scid and scon: | ||
Line 33: | Line 35: | ||
mIRC replaces $* by the marker, but scon has an extra evaluation system which fits perfectly. The $* mechanism is enabled on scon, and mIRC stores the command of the $* as "scon -r echo -a $!mid( `~$* ,2)" | mIRC replaces $* by the marker, but scon has an extra evaluation system which fits perfectly. The $* mechanism is enabled on scon, and mIRC stores the command of the $* as "scon -r echo -a $!mid( `~$* ,2)" | ||
− | + | Then, mIRC evaluates the line for each token, which then becomes: "scon -r echo -a $mid( abcd ,2)", and finally the scon command is executed, resulting in the expected value being echoed. | |
Another issue is that you cannot call $* more than once in the same scope; the command will simply be skipped. However, this can also be circumvented/worked-around :) | Another issue is that you cannot call $* more than once in the same scope; the command will simply be skipped. However, this can also be circumvented/worked-around :) |
Revision as of 18:44, 3 September 2015
The $* allows you to iterate over all of the tokens contained within $1-. The way this works is much like a while loop. $* has been omitted from the help file since while loops were introduced.
$* is extremely powerful, because it is much faster than using a while loop on a list of tokens. Understanding how this works is pretty simple, so consider the following:
var %list a b c d,%a 1 while ($gettok(%list,%a,32)) { echo -a $v1 inc %a }
This can be written as:
tokenize 32 a b c d | echo -a $*
The above is much smaller, and a lot faster. Pretty cool, isn't it?
Notes & quirks
Why was the $* identifier removed from the help file in the first place? Well, $* was removed because it does not really work the same way that the other identifiers do, and because it is quirky.
Quirky? Quirky, how?
Well, mIRC takes the command $* appears in, and replace all of the occurrences in the line by a special marker: `~$*: You can see that by doing for example:
//tokenize 32 abcd | echo -a $left($*,1) $+ $chr(3) $+ $mid($*,2)
Let's take a look at this and understand why the above return such a value:
//tokenize 32 abcd | echo -a $mid($*,2)
Why isn't this returning bcd? Because of the usage of the special marker `~$*, mIRC has stored that the command is actually "echo -a $mid(`~$*,2)".
Then, for each token, (here only $1 == abcd), mIRC evaluates the line, here $mid(`~$*,2) becomes ~$*, and then mIRC replaces the marker by the token and executes the echo command. However, after an operation, like $mid here, that marker cannot be found;
Basically, it cannot be guaranteed to get the correct value of $* inside an identifier.
There is a workaround for the above issue, and that workaround is by using scid and scon:
//tokenize 32 abcd | scon -r echo -a $!mid( $* ,2)
mIRC replaces $* by the marker, but scon has an extra evaluation system which fits perfectly. The $* mechanism is enabled on scon, and mIRC stores the command of the $* as "scon -r echo -a $!mid( `~$* ,2)"
Then, mIRC evaluates the line for each token, which then becomes: "scon -r echo -a $mid( abcd ,2)", and finally the scon command is executed, resulting in the expected value being echoed.
Another issue is that you cannot call $* more than once in the same scope; the command will simply be skipped. However, this can also be circumvented/worked-around :)
Simply retokenize after using $*, which forces the update of $1-, $* can be used again, but only if the number of tokens passed is higher than the previous number of tokens passed. $* will only start from the previous number of tokens + 1:
//tokenize 32 1 2 3 | echo -a $* | tokenize 32 4 5 6 7 8 | echo -a $* | echo -a here
The first three tokens of the second tokenize, "4 5 6", are dummy tokens which are passed to fill in the gap. $1- can be used in front of the next tokenize command in order to start from those new tokens, much like a normal situation.