mIRC Home    About    Download    Register    News    Help

Print Thread
Help, Telling if a %var is Global or Local #159811 20/09/06 10:07 AM
Joined: Dec 2002
Posts: 43
C
Cypris Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Dec 2002
Posts: 43
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.


-Cypris
Re: Help, Telling if a %var is Global or Local #159812 20/09/06 11:12 AM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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.

Re: Help, Telling if a %var is Global or Local #159813 20/09/06 07:37 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
cant you use /set %var %var

Re: Help, Telling if a %var is Global or Local #159814 20/09/06 07:38 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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

Re: Help, Telling if a %var is Global or Local #159815 20/09/06 08:29 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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 %}

Re: Help, Telling if a %var is Global or Local #159816 20/09/06 08:38 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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.

Re: Help, Telling if a %var is Global or Local #159817 20/09/06 09:36 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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!

Re: Help, Telling if a %var is Global or Local #159818 20/09/06 09:41 PM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Help, Telling if a %var is Global or Local #159819 20/09/06 09:44 PM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Help, Telling if a %var is Global or Local #159820 21/09/06 04:41 AM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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 }
}

Re: Help, Telling if a %var is Global or Local #159821 21/09/06 08:41 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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.

Re: Help, Telling if a %var is Global or Local #159822 21/09/06 09:34 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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??

Re: Help, Telling if a %var is Global or Local #159823 22/09/06 07:10 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
Stunned noone mentioned this:
Code:
alias local { 
  var %x = 1
  set %y 1
  echo -a $var(%y,1).local $var(%x,1).local
}


$maybe
Re: Help, Telling if a %var is Global or Local #159824 22/09/06 07:49 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
I can't remeber Everything! smile

Re: Help, Telling if a %var is Global or Local #159825 05/10/06 02:09 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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