mIRC Homepage
Posted By: Cypris Help, Telling if a %var is Global or Local - 20/09/06 10:07 AM
This may sound odd...but I need my script to KNOW if a var passed to it is global (/set) or local (/var)?

I suppose i could make an alias called /set and have it write to an ini , the variable name as ITEM and Global as VALUE, but then i would need to have an alias for unset and yada yada. (the same for /var too)

But CMON! There has to be a way! $var() searches both var types,

The reason i need to do this is because i am writting an alias called VADD which adds the string/data to the end of the variable instead of overwritting it, and if i pass it a local var, i dont want to /set it, i want to /var %var = %var $+ %newdata.

Any ideas. i really hope i overlooked something and this is more simple than i think it is.
Posted By: hixxy Re: Help, Telling if a %var is Global or Local - 20/09/06 11:12 AM
Code:
alias varexists { return $iif($eval($1,2) != $null,$true,$false) }


//var %x = test | echo -a $varexists(% $+ x) | set %x test | echo -a $varexists(% $+ x) | unset %x

This will not check if a local variable exists, it will return $false if the variable doesn't exist at all or is local, but I think that's the best you can do.
cant you use /set %var %var
Posted By: hixxy Re: Help, Telling if a %var is Global or Local - 20/09/06 07:38 PM
As long as it's declared first.

Code:
var %var
; %var = <value> or /set %var <value> will both set the value of the local %var
Posted By: DaveC Re: Help, Telling if a %var is Global or Local - 20/09/06 08:29 PM
alias isglobal { return $iif($var($1),$true,$false) }

I think that would be better, dont malfunction if the global exists but contains $null or text litteral "$null" also doesnt care if u go $isglobal(% $+ var) or $isglobal(var) {with no %}
Posted By: hixxy Re: Help, Telling if a %var is Global or Local - 20/09/06 08:38 PM
Quote:
dont malfunction if the global exists but contains $null


That would have 0 uses anyway :tongue: I see what you mean about the text $null though.
Posted By: DaveC Re: Help, Telling if a %var is Global or Local - 20/09/06 09:36 PM
Quote:
That would have 0 uses anyway :tongue: I see what you mean about the text $null though.


LOL I know it wouldnt have almost any uses, well none i could think of at least. The string litteral "$null" caught me out once and i didnt work it out for ages, i was processing a load of stuff to make a composite line, and looping till i got a blank line, not expecting "$null" on a line frown

This is interesting tho, as i think i shopuld have read what the orginal poster anted to do anyway...
Quote:
The reason i need to do this is because i am writting an alias called VADD which adds the string/data to the end of the variable instead of overwritting it, and if i pass it a local var, i dont want to /set it, i want to /var %var = %var $+ %newdata.


I assume he means hes passing the variable name to the alias, so hes passing by refrence rather than value, and if thats the fact then he cant pass a local var anyway, as it wont exist in the alias he passed it too, ie: there would be no point in doing "/var %var = %var $+ %newdata" becuase as the alias exits %var well be destroyed, not that it would have had any value before the /var that created it in the alais anyway!

why he doesnt just use %var = $+(%var,%newdata) seems a bit off to me!
local variables aren't carried over from alias to alias, even if one calls the other :tongue: so, if in your alias named VADD you want to modify the value of a variable that is local to the parent alias, you'll find it's not possible :s unless you return a piece of data to the parent instructing it to declare the variable locally

for example:

Code:
alias VADD {
........
 return set $(%var %var $+,) %newdata
}


then in your parent alias call $($vadd(),2) as a command

as hixxy said, /set first looks for local variables to change the value of, then moves on to global ones if none is found. this should be just what you need shocked
Quote:

why he doesnt just use %var = $+(%var,%newdata) seems a bit off to me!


i was wondering the same lol. maybe VADD contains a lot of code, that he's calling from more than one alias :> if not, it should certainly be done away with
rereading the OP I should have said
/set %var %var $1
he was saying he wanted to pass the value of a local var to another alias that would add that to a global var
alias lvar {
var %one = $?
if (%one == test) { addon %one }
}
alias addon {
set %two %two $1
}

or just

alias lvar {
var %one = $?
if (%one == test) { set %two %two %one }
}
Posted By: DaveC Re: Help, Telling if a %var is Global or Local - 21/09/06 08:41 AM
pardon? lol

I read that he thinks he can pass a variable by refrence rather than how mirc actually passes all info by value, he said "if a var passed to it is global (/set) or local (/var)", since the var is passed not as the var but as its contents, there is no back refrence possable. Unless of course hes passing the litteral text "%var" and then hes got the problem with it not existing as a local in the alias anyway, so could detect a global but not if it was a local in the calling alias.
true, so I guess the only way to tell is check the var has a value before passing it $($+(%,var),0) and if it has no value after being evealuated twice in the alias then being $null it was a local var??
Stunned noone mentioned this:
Code:
alias local { 
  var %x = 1
  set %y 1
  echo -a $var(%y,1).local $var(%x,1).local
}
I can't remeber Everything! smile
Posted By: DaveC Re: Help, Telling if a %var is Global or Local - 05/10/06 02:09 AM
LOL just came back to this thread to mention that, now that i finally read it in the help (/me wonders how long its been in there for since i never saw it before, likely longer than i amagine!)

I think the thread got of the first page, whcih normally ends it for me, otherwise i would have seen your reply!

Something else i saw thats nice
Code:
alias ex { 
  set %x global | var %x = local
  echo -a $var(%x,1) = $var(%x,1).value ... is it local? $var(%x,1).local
  echo -a $var(%x,2) = $var(%x,2).value ... is it local? $var(%x,2).local
}

/ex
%x = local ... is it local? $true
%x = global ... is it local? $false
© mIRC Discussion Forums