mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2018
Posts: 83
E
Babel fish
OP Offline
Babel fish
E
Joined: Apr 2018
Posts: 83
Could I ask about the scope of variables created with the /var <variable name> ?

I was researching something else and looking for clues all over the place and (re-) stumbled upon this from the mIRC help (.chm) file :

Local variables are variables that exist only for the duration of the script in which they are created and can only be accessed from within that script. They can be created with the /var command:

which suggests local variables are accessible anywhere within a script.

Wikichip variables - mIRC expands upon this with :
Local Variables are given local scope. They are created for the duration of the script that made them and they can only be accessed from within the code line, or block in which they were created in. Once the script has finished, the variable is deleted.

Now that agrees and then disagrees with the help file.

'they can only be accessed from within the code line' - that has to be wrong. If you can only access it from 'the line' then you cant use it on any other line, or does that refer to a one line 'code block' ? In which case - why mention it ?

'or block in which they were created in.' makes more sense and agrees with (just about) every other languages, but then follows that with :

'Once the script has finished, the variable is deleted.'
I thought it was local to the code block (alias ?) In which case it would be deleted as soon as the code block is finished with and recreated if the code block was called again.

Now that raised another question.
Just what is a 'code block' ? I had assumed it was an alias, or an On Event block, and have some global variables to allow access between the different code blocks.
'until the script has finished' suggests that /var items are 'Global' within the script.

Could some one please defing local scope in an understandable manner, devoid of contradictions ?

Last edited by Erasimus; 19/10/18 01:06 AM.
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
In this context, the "duration of the script" pertains to any particular activity that has been initiated -- whether a remote event or a popup menu or a timer firing or a typed out /alias.

Not to be confused with "script file." There are no script-file scope variables, yet.

Local variables die within a child alias when it returns to its parent alias/event, or when the top-level alias/event reaches its end or is prematurely /returned or /halted, etc. That's what we're calling a "script finished."

If you need a variable to survive from child to parent, you can use binary variables, or hash table entries, or global variables. A global variable /set -u0 %name data will automatically unset when the top-level alias/event has terminated. You can name temporary global variables within an event with that event's unique $eventid to avoid collisions. You can use this same trick with /hadd -u0 for self-destructing hash table entries. Binary variables are global in nature, in that they transcend parent to child to parent, but automatically self-destruct when an alias/event finishes.

Last edited by Raccoon; 19/10/18 06:23 AM. Reason: last paragraph: binary variables are global in nature, in that they...

Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Apr 2018
Posts: 83
E
Babel fish
OP Offline
Babel fish
E
Joined: Apr 2018
Posts: 83
Thanks Racoon, that cleared things up and was just as I expected.

The documentation for this is (ahem) not very helpful, and, in both places, needs to be corrected, and it was those sources that started the confusion in my head.

Thanks for the additional info in your last paragraph, I didn't know about those items.

Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
I suspect that the using of "code line" here was probably intended to refer to a //command in an editbox. For example, if you were to execute this line in an editbox:

Code:
//unset %ticks | var %ticks $ticks , %i 99999 | while (%i) { var %a $rand(a,z) | dec %i } | echo -a $calc($ticks - %ticks)



... you can see that the last command of that "code line" still knows the value of %ticks created at the beginning of that line. But if you then have another "code line" of "//echo -a %ticks" it can't see the value of %ticks.

Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
Originally Posted By: Erasimus
Thanks for the additional info in your last paragraph, I didn't know about those items.
You're welcome. I just amended to the last line of the last paragraph.

If you're trying to retrieve several values(variables) from a child alias to its parent, one way is to call it as an identifier and have it /return its several values as a tokenized string. You can then split apart the string with the /tokenize command to access each value as $1 $2 $3, etc.

Code:
ALIAS ParentAlias {
  tokenize 10 $ChildAlias(42)
  echo -a Addition: $1 , Multiplication: $2 , Exponent: $3
}
ALIAS ChildAlias {
  return $+($calc($1 + $1),$LF,$calc($1 * $1),$LF,$calc($1 ^ $1))
}


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Jul 2006
Posts: 4,144
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,144
I have edited the wikichip page to be clearer and added a mention about /set -u, which I called semi global.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Apr 2018
Posts: 83
E
Babel fish
OP Offline
Babel fish
E
Joined: Apr 2018
Posts: 83
Thanks Wims.

Racoon, all that is excellent information and I will keep note of it, but it wasn't necissarily what I meant.

I have a global variable called %serverregistration that gets set to 1 on receiving raw 1 message from the IRC server, and is only reset after the user counts on the server messages.

That way my On raw events can happily mask the outputs I dont want to see,. In the case of MotD, that gets written to a .txt file (converting space to $chr(160) - remeber that post ?) checking the file crc and comparing it with the last saved MotD (another glob stores the crc), then only displays it in a custom window if different, but ONLY if this is in server registration.

Once thats all done and dusted, channels joined etc. and the user types /motd into (say) the status window: that processing just doesn't happen - proceedes as per mIRC default (assuming skip MotD is not not set in Options, IRC, Options).

Same for LUSERS and Names <Channel Name> (diff variable for that one - masked during connection process, but can be entered by users and displayed.

No child to parent in that type of example, but the discussion got good info out of it smile

Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
*self-destructing global variable

semi-global makes it more confusing :P


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Jul 2006
Posts: 4,144
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,144
Aka Glocal variables.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel

Link Copied to Clipboard