mIRC Home    About    Download    Register    News    Help

Print Thread
Page 1 of 2 1 2
Local variables #171118 19/02/07 06:39 PM
Joined: Jan 2007
Posts: 259
K
Kardafol Offline OP
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
It would be nice to have a command that should be followed by another command, and would allow the alias/event calling the command to keep any local variables set by the called alias.
Example (above is most likely unclear :P):
Code:
on *:text:*:*: {
 keepvars getinfo $nick
 if ($1 == %localvar) {
  .msg $iif($chan,$chan,$nick) %response
 }
}
alias -l getinfo {
 if ($hget(info,$1)) {
  var %localvar = $gettok($v1,1,32), %response = $gettok($v1,2-,32)
 }
}



Those who can, cannot. Those who cannot, can.
Re: Local variables [Re: Kardafol] #171123 19/02/07 07:11 PM
Joined: Sep 2005
Posts: 2,876
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,876
Just use /set -u (with no timer) instead of /var.

Re: Local variables [Re: hixxy] #171131 19/02/07 09:56 PM
Joined: Jan 2007
Posts: 259
K
Kardafol Offline OP
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
But what if a global variable is already set with the same name?


Those who can, cannot. Those who cannot, can.
Re: Local variables [Re: Kardafol] #171132 19/02/07 10:35 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
The most recent set of the variable, based on the name, takes priority.

It is (currently) not possible to have a local variable and a global variable with the same name (and I would suspect different values).

Re: Local variables [Re: Kardafol] #171133 19/02/07 10:45 PM
Joined: Sep 2005
Posts: 2,876
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,876
You should give your variables unique names anyway.

Making a script use variables like %title and %name is not a good idea. You should use %yourscriptname.name and %yourscriptname.title

Re: Local variables [Re: RusselB] #171134 19/02/07 11:02 PM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
Originally Posted By: RusselB
The most recent set of the variable, based on the name, takes priority.

It is (currently) not possible to have a local variable and a global variable with the same name (and I would suspect different values).

it's possible, and you can use $var() to select which var to take the value of. although it sure is a recipe for confusion!


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Local variables [Re: RusselB] #171138 20/02/07 12:04 AM
Joined: Jan 2007
Posts: 259
K
Kardafol Offline OP
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
Code:
alias vartest {
  echo -a Setting % $+ vartest.blah to 123
  set %vartest.blah 123
  echo -a % $+ vartest.blah: %vartest.blah
  echo -a Calling vartest.a
  vartest.a
  echo -a % $+ vartest.blah: %vartest.blah
  unset %vartest.blah
}
alias -l vartest.a {
  echo -a (Called alias) Setting local var % $+ vartest.blah to 123456
  var %vartest.blah = 123456
  echo -a (Called alias) % $+ vartest.blah: %vartest.blah
}

You can have two (or more) variables with the same name, unlimited local and one global.


Those who can, cannot. Those who cannot, can.
Re: Local variables [Re: Kardafol] #171156 20/02/07 05:53 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I stand corrected. Personally I prefer to use variables that are distinctive or very temporary (eg: using %a and %b as counters in a while loop).

Re: Local variables [Re: RusselB] #171177 20/02/07 08:03 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
so my question then is say on a join event I have it run through a while loop and use var %a and %b.

If someone joins while this while loop is going, will this overwrite the variables %a and %b?

I mean, I haven't had any issues, but by what is said here they would, wouldnt they?

Re: Local variables [Re: DJ_Sol] #171180 20/02/07 08:45 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
No, they'd still have the expected values.

For one thing mIRC's scripting language is single-threaded so only one alias/event/whatever is processed at any given time, meaning if someone joins while your code is looping the join event code won't be triggered until after the loop has finished and the code block it's in has ended.

Even if mIRC scripting were multi-threaded it wouldn't make any difference (in theory) as long as they were local variables in which case they're effectively different variables for each scope in which they're used.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: Local variables [Re: starbucks_mafia] #171182 20/02/07 09:11 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
So it makes a queue and the 2nd join event waits for the first to finish before executing? nice to know.

Re: Local variables [Re: DJ_Sol] #171190 20/02/07 09:39 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
Basically, though it's fast enough that you shouldn't ever notice.


Invision Support
#Invision on irc.irchighway.net
Re: Local variables [Re: Kardafol] #171206 21/02/07 02:18 AM
Joined: Jul 2003
Posts: 655
Om3n Offline
Fjord artisan
Offline
Fjord artisan
Joined: Jul 2003
Posts: 655
I guess i personally dont see the need, whats wrong with doing something like this...

Code:
on *:text:*:*: {
 var %info = $getinfo($nick)
 if (!%info) return
 if ($1 == $gettok(%info,1,32)) {
  .msg $iif($chan,$chan,$nick) $gettok(%info,2-,32)
 }
}
alias -l getinfo {
 if ($hget(info,$1)) { return $v1 }
}


"Allen is having a small problem and needs help adjusting his attitude" - Flutterby
Re: Local variables [Re: Om3n] #171219 21/02/07 08:17 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Good Point. I understand you were using a simple example but me in my nit pickiness would have to say.

var %info = $iif($hget(info,$nick),$ifmatch,$false)

Re: Local variables [Re: DJ_Sol] #171224 21/02/07 11:06 AM
Joined: Jul 2003
Posts: 655
Om3n Offline
Fjord artisan
Offline
Fjord artisan
Joined: Jul 2003
Posts: 655
Indeed i wasn't claiming that was the best or even easiest, just a quick edit to demonstrate my point. Personally cant think of a situation at this time where this suggestion would offer a significant improvement, then again everybody is always asking for a for or foreach function.


"Allen is having a small problem and needs help adjusting his attitude" - Flutterby
Re: Local variables [Re: Om3n] #171228 21/02/07 12:41 PM
Joined: Jan 2007
Posts: 259
K
Kardafol Offline OP
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
That was just a simple example, but what if there were multiple results?
Code:
on *:text:*:#: {
....
calling somealias...
....
}
alias -l somealias {
...lost of complex stuff here...
var %var1 = ..., %var2 = blah blah, %var3 = somthing else, %var4 = ..., %varX = ...
...
}

Then the only way to solve it would be hash tables, but it would be slower to make a hash table, and populate it with the data.
It would be nice to be able to return an array.
However set -u0 does this, but it's somewhat messy. Also, all the lenghts of the variables differ, and the number of spaces/etc they contain is also different. /tokenize wouldn't work, nor would $gettok.
Unless escaping all the spaces, pipes or whatever, or using hash tables, or global variables (which would make it really messy, and hard to read), this wouldn't be possible. Also, not all the variables would be taken from a hash table. Some of them might have been calculated or altered in some way.

Last edited by Kardafol; 21/02/07 12:42 PM.

Those who can, cannot. Those who cannot, can.
Re: Local variables [Re: Kardafol] #171293 22/02/07 12:05 PM
Joined: Jul 2003
Posts: 655
Om3n Offline
Fjord artisan
Offline
Fjord artisan
Joined: Jul 2003
Posts: 655
You can still use gettok, you just have to place a special token between each for the return... (chr9 is a tab, but you can use any chr you like)

return $+(%var1,$chr(9),%var2,$chr(9),%varN)
var %result = $somealias()
$gettok(%result,X,9)

OR if you dont need the original $1- anymore you can use tokenize

return $+(%var1,$chr(9),%var2,$chr(9),%varN)
tokenize 9 $somealias()


"Allen is having a small problem and needs help adjusting his attitude" - Flutterby
Re: Local variables [Re: Kardafol] #171299 22/02/07 02:22 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
I am curious why the length of the variable matters for using $gettok. You can easily find the length with $numtok or $gettok.

Anyhow, arrays being added don't bother me, but I still don't really see very much use for them. No example that has ever been given in he many threads about arrays has ever been that much better (imo) than existing methods.


Invision Support
#Invision on irc.irchighway.net
Re: Local variables [Re: Riamus2] #171302 22/02/07 03:20 PM
Joined: Jan 2007
Posts: 259
K
Kardafol Offline OP
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
With arrays, you can pass data from a alias to alias, without needing to use $gettok, hash tables, etc. Its alot simpler than using hash tables, and alot easier aswell.

Also, $gettok conflicts if there is a character in the data thats used as a deltimer.

Last edited by Kardafol; 22/02/07 03:22 PM.

Those who can, cannot. Those who cannot, can.
Re: Local variables [Re: Kardafol] #171307 22/02/07 04:22 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Even if arrays are added I doubt they'll be any easier to pass between aliases than hash tables, ie. you'll still need to pass the name of the array and then the called alias will need to know that it's an array name and use $array($1, <index>) or whatever. I don't see any reasonable way around that. The only benefit of arrays would be the possible efficiency gained in certain operations (eg. iterating over them). For your purposes right now you could still use a hash table or global variables, all you'd need to do is some name-checking to make sure the temporary variables/hashtable don't already exist, then return the temporary name(s).

Code:
on *:text:*:*: {
  var %vars = $getinfo($nick)
  var %local = $tempget($gettok(%vars,1,32)), %response = $tempget($gettok(%vars,2,32))
  if ($1 == %local) {
    .msg $iif($chan,$chan,$nick) %response
  }
}
alias -l getinfo {
  var %varname1 = $tempvar(get1), %varname2 = $tempvar(get2)
  if ($hget(info,$1)) {
    tempset %varname1 $gettok($v1,1,32)
    tempset %varname2 $gettok($v1,2-,32)
  }
  return %varname1 %varname2
}

alias tempvar {
  var %tmp = $+(%,temp.,$md5($rand(1,10000) $ticks $1))
  while ($var( [ %tmp ] ,1)) %tmp = $+(%,temp.,$md5($rand(1,10000) $ticks $1))
  return %tmp
}
alias tempset set -u0 $1-
alias tempget return $eval($iif($left($1,1) == %,$1,$+(%,$1)),2)


Not exactly pretty but it works and it definitely won't affect other global variables.

It just uses three extra aliases, $tempvar to create a new alias name, /tempset to set the temp variable's value from it's local variable name, and $tempget to get the variable's value.

As you can probably see /tempset and $tempget are just vanity functions to clean-up the code a tiny bit.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Page 1 of 2 1 2