mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
anyone got a good explanation document on usage of [ ] i still end up doing alot of them by trail and error.

ie:
/test {
set %asd blah
set %asd $+ 1 one
set [ %asd $+ 2 ] two
set %qwe1 %asd $+ 1
set %qwe2 [ %asd $+ 2 ]
set %qwe3 [ % $+ asd $+ 2 ]
set %qwe4 [ [ % $+ asd $+ 2 ] ]
set %qwe5 $+(%,asd,2)
set %qwe6 [ $+(%,asd,2) ]
set %qwe7 [ [ $+(%,asd,2) ] ]
}

results with....

%asd blah
%asd1 one
%asd2 two
%qwe1 blah1
%qwe2 blah2
%qwe3 blah2
%qwe4 blah2
%qwe5 %asd2
%qwe6 %asd2
%qwe7 two


i would have thought
set %qwe4 [ [ % $+ asd $+ 2 ] ]
&
set %qwe7 [ [ $+(%,asd,2) ] ]
would have made the same result, but no frown


Joined: Mar 2003
Posts: 612
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Mar 2003
Posts: 612
i dont even use the [] square brackets any more, i like $eval it seems better and easier to read/code ;o)


billythekid
Joined: Jan 2003
Posts: 1,063
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2003
Posts: 1,063
everything between [ ] will be evaluated.

so:

/var %test = foo
/var %test1 = bar
/var %number = 1
echo %test $+ %number >> foo1
echo %test [ $+ [ %number ] ] >> bar


If it ain't broken, don't fix it!
Joined: Aug 2003
Posts: 1,831
I
Hoopy frood
Offline
Hoopy frood
I
Joined: Aug 2003
Posts: 1,831

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
I don't have any doc on that (there used to be one at the old mirc.net, written by Ntd, but it no longer exists) but I'll try to explain myself what the [ ] are for (at least how I understand it).

The known (and fairly well documented) properties of [ ] are:

1) they control the order of evaluation
2) they can re-evaluate single variables/identifiers, like $eval(%var/$ident,2)

I'll give you a couple of examples on these before I go on with the other, more peculiar property of [ ].

1) To observe the order of evaluation, we'll use this alias:
Code:
print if !$0 { tokenize 32 1 } | echo 3 -a echo from print( $+ $1-) alias | return value of print( $+ $1-)
If you type
//echo -a $print(1) [ $print(2) ] $print(3)
you'll notice that $print(2) gets evaluated first. The [ ] can be nested too:
//echo -a $print(1) [ $print(2.1) $print(2.2) [ $print(2.3) ] ] $print(3)
$print(2.3) gets evaluated first

2) Their ability of [ ] to re-evaluate vars/idents is pretty straightforward. The only thing that you should remember is that it only works where there is only a single variable or identifier inside the [ ]. Example:
//echo -a [ [ $!version $!version ] ]
returns "$version $version"

//echo -a [ [ $!version ] ]
returns "6.12"


Now, the weird (and not clearly documented) part. The $+ identifier (NOT the $+(), which is newer) behaves in a totally different way when inside [ ]. In normal code, the order of evaluation in any expression v1 $+ v2is this:

1) v1 gets evaluated
2) v2 gets evaluated
3) the values of v1 and v2 are concatenated by $+

Again, we'll use the $print alias to observe this:
//echo -a $print(1) $+ $print(2)

Inside [ ] though, things are different. In any expression like the above

1) v1 gets evaluated
2) the value of v1 and the UNEVALUATED v2 are concatenated by $+
3) the resulting string gets evaluated

To observe this one, we'll use another alias:
Code:
alias p%test echo 5 -a echo from p%test | return value of p%test
The % char inside the alias name is deliberate (and perfectly accepted by mirc). Type this:
//var %test | echo -s [ foo $!p $+ %test bar ]
You get "foo <value of $p%test> bar". Let's examine it step by step:

1) v1 gets evaluated
v1 here is "$!p". When it's evaluated, the result is the string "$p"

2) the value of v1 and the UNEVALUATED v2 are concatenated by $+
"$p" is concatenated with "%test", which did NOT get evaluated (if it did, it would be $null)

3) the resulting string gets evaluated
the resulting string here is "$p%test", which gets evaluated


This behaviour is one of the reasons that the well-known %blah [ $+ [ %bleh ] ] method of retrieving dynamic variables works, although it may not be obvious at first. Let's analyze this example:
//var %blah32 = tada | echo -a %blah [ $+ [ $bits ] ]

Here, we have a case similar to [ v1 $+ v2 ], except that v1, which here is "%blah", is outside the outer [ ]. For this reason, v1 won't be evaluated first (like "$!p" did in the previous example); the outer [ ] gives priority to its contents. So step (1) is skipped. In step (2), v2 is $bits, but unlike "%test" from the previous example, it is enclosed in [ ] itself (nested brackets). So, "$bits" will not be left unevaluated (like "%test" did) prior to its concatenation with v1 (ie "%blah"). So, we have the string "%blah" concatenated with the value of $bits, which is 32. Finally, step (3), which is the evaluation of the resulting string "%blah32", follows. Tada!


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Feb 2003
Posts: 810
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Feb 2003
Posts: 810
Although I already know all of this, I'd never be able to explain evaluation brackets like you just did. I'm going to save your post here so I could use it to teach others. Thanks smile :tongue:


* cold edits his posts 24/7
Joined: Aug 2003
Posts: 1,831
I
Hoopy frood
Offline
Hoopy frood
I
Joined: Aug 2003
Posts: 1,831
Good post! laugh

Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Thanks that was a really good post. cleared up some confusions i have had, also unfortunitly lead me to discover some other odd ones frown, the biggest problem i have is the behavour of what happens with the [ ] doesnt seem to remain constant. as an example i give you this.

test {
set %blah AAAA
set %what BBBB
set [ %blah $+ %what ] Result1
set %odd [ %blah $+ %what ] Result2
}

results....
%blah AAAA
%what BBBB
%blah%what Result1
%odd AAAA%what Result2

the same [ %blah $+ %what ] was evaluated differently based on where/how it is used within the command.


Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Ah, that's not an inconsistency of [ ], it's because of /set. When /set is used, the parser is directed to not evaluate the 1st parameter if it is a variable. In other words, if the first word of a line is "set", the second word is not evaluated, if it's a var. That's how //set %var is able to set variables. This also happens with /inc and /dec.

Note that the first word need not be a literal "set"; it can be a variable or an identifer that returns "set"!
Code:
alias testset var %a = set | %a %b bleh | echo -a %b
If you type /testset, "bleh" is echoed.

So, in your example, [ %blah $+ %what ] is correctly evaluated to "AAAA%what" except when it's right after a /set command. The parser then evaluates the expression [ %blah $+ %what ] as always, except that it doesn't evaluate %blah. To get %blah to be evaluated with /set (in case %blah's value is another variable name), you need to

1) enclose %blah in another pair of [ ] to force premature evaluation.
//unset %*%* | var %a = % $+ b | set [ [ %a ] $+ %c ] bleh | echo -s %b%c

or

2) put it inside an identifier, so that the parser doesn't see the variable name and skip evaluation.
//unset %*%* | var %a = % $+ b | set [ $(%a) $+ %c ] bleh | echo -s %b%c


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com

Link Copied to Clipboard