mIRC Home    About    Download    Register    News    Help

Topic Options
#263992 - 19/10/18 02:05 AM Scope of local variables - a query
Erasimus Offline
Ameglian cow

Registered: 26/04/18
Posts: 34
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 ?


Edited by Erasimus (19/10/18 02:06 AM)

Top
#263993 - 19/10/18 02:31 AM Re: Scope of local variables - a query [Re: Erasimus]
Raccoon Online
Hoopy frood

Registered: 18/02/03
Posts: 2501
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.


Edited by Raccoon (19/10/18 07:23 AM)
Edit Reason: last paragraph: binary variables are global in nature, in that they...
_________________________
doin� things a particle can

Top
#263995 - 19/10/18 03:32 AM Re: Scope of local variables - a query [Re: Raccoon]
Erasimus Offline
Ameglian cow

Registered: 26/04/18
Posts: 34
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.

Top
#263996 - 19/10/18 04:36 AM Re: Scope of local variables - a query [Re: Erasimus]
maroon Offline
Hoopy frood

Registered: 12/01/04
Posts: 969
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.

Top
#263999 - 19/10/18 07:42 AM Re: Scope of local variables - a query [Re: Erasimus]
Raccoon Online
Hoopy frood

Registered: 18/02/03
Posts: 2501
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))
}
_________________________
doin� things a particle can

Top
#264008 - 19/10/18 03:36 PM Re: Scope of local variables - a query [Re: Erasimus]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3436
Loc: France
I have edited the wikichip page to be clearer and added a mention about /set -u, which I called semi global.
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#264015 - 20/10/18 04:48 AM Re: Scope of local variables - a query [Re: Wims]
Erasimus Offline
Ameglian cow

Registered: 26/04/18
Posts: 34
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

Top
#264017 - 20/10/18 10:31 AM Re: Scope of local variables - a query [Re: Wims]
Raccoon Online
Hoopy frood

Registered: 18/02/03
Posts: 2501
*self-destructing global variable

semi-global makes it more confusing :P
_________________________
doin� things a particle can

Top
#264018 - 20/10/18 10:35 AM Re: Scope of local variables - a query [Re: Raccoon]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3436
Loc: France
Aka Glocal variables.
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top