Variable reader ($vget)

By Savage_CL on Dec 26, 2011

I wrote this alias to simplify reading dynamic variables. It uses a format similar to token identifiers. I'll give a few examples.

In these examples, the following are true:

  • Your nick is: Savage
  • you are on SwiftIRC
  • This was called via an on TEXT event
  • The person who activated it is: Jaytea
  • It was activated in: #Savage_CL

This is your variable file:

%testSavage hello
%call~Jaytea~#Savage_CL world
%this.SwiftIRC.#Savage_CL mychan

Note that the first doesn't have a seperator, the second uses ~ which is $chr(126), and the third uses a period, which is $chr(46).

Evaluations will be as follows:

$vget(test,$me,0) == hello
$vget(call,$nick,$chan,126) == world
$vget(this,$network,$chan,46) == mychan

You see that using 0 will cause the snippet to not use a seperator.

If you are writing a script that uses this identifier to call a variable multiple times without changing it, I suggest doing something like the following:

var %foo = $vget(foo,$bar,46)

This is because the alias uses functions that aren't particularly fast (like the while loop) and it would be faster to use it only when needed.

If you find this useful or find any bugs, visit me in #Savage_CL on SwiftIRC or comment here.

Thanks!

PS I know this is a lot longer than it has to be and I could likely have done it in 3 lines, but I wanted to do it this way. It makes it easier to read in my opinion.

alias vget {
  if (!$2) { echo -ea invalid parameters! | HALT }
  var %toks = $0,%items = %toks - 1,%tokenizer = $ [ $+ [ %toks ] ],%t = 1,%name = $chr(37) $+ $ [ $+ [ %t ] ],%t = 2
  while (%t <= %items) {
    var %name = %name $+ $iif(%tokenizer,$chr(%tokenizer)) $+ [ $ [ $+ [ %t ] ] ]
    inc %t
  }
  if ($(%name,2) != $null) {
    return $v1
  }
  else {
    echo 7 -ae %name is not set!
  }
}

Comments

Sign in to comment.
Savage_CL   -  Nov 27, 2012

@Spoof
Just letting you know that I was, in fact, right. With echo, the color comes before the switches.

 Respond  
hixxy   -  Jan 14, 2012

Spoof: echo 7 -ae is the correct format...

 Respond  
hixxy   -  Jan 13, 2012

WorldDMT: I've done it that way because I see little point in specifying an ascii number when you can just put the actual character in the parameters. For example, say I have a variable called %text.#hawkee.hixxy, it's easier to use $vget(text.,$chan,.,$me) than it is to have to specify the ASCII number!

Savage_CL  -  Nov 01, 2013

What if you wanted to use a comma? And wouldn't you think consistency is a good thing? for example, do we use $numtok(%string,.) ?

Sign in to comment

Spoof   -  Jan 13, 2012

line 12 has an error:
echo 7 -ae %name is not set!
The color should be after -ae:
echo -ae 7 %name is not set!

Or else its going to echo -ae along with %name is not set! in orange.

 Respond  
WorldDMT   -  Jan 12, 2012

@hixxy: ?!

 Respond  
hixxy   -  Jan 11, 2012

Good idea but could be simplified considerably.

alias vget { return $(% $+ $remove($1-,$chr(32)),2) }
 Respond  
Jethro   -  Dec 28, 2011

I know that, for alias to return a calling routine, you don't need to indicate the dash beside $1, but to trigger directly from the alias and return the rest of tokens, you need the dash.

 Respond  
WorldDMT   -  Dec 28, 2011

@FelicianoX your EG is off topic

btw try to do this

alias test {
  echo -a $1-
  return $1
}
 Respond  
Jethro   -  Dec 28, 2011

Oh I see. To get the value though, it is: $var(%test,1).value

 Respond  
Firstmate   -  Dec 28, 2011

He means like %test hello, $var(%test hello)

 Respond  
Jethro   -  Dec 28, 2011

variables can't have spaces in them.Really?

 Respond  
FelicianoX   -  Dec 28, 2011

WorldDMT, when you have an identifier it's not automatically tokenized 32, try (alias test return $1), and do echo -a $test(hello there, this wont show).

For this script you don't need to tokenize 32 because variables can't have spaces in them.

 Respond  
Fuzionx   -  Dec 28, 2011

Yeah, I misunderstood the script a bit. I thought you had to use something like $vget(%var,$me) etc lol.

 Respond  
Firstmate   -  Dec 28, 2011

@WorldDMT: Instead of looking at the examples, if you look at the snippet, you'll see he loops around $0.

 Respond  
WorldDMT   -  Dec 28, 2011

when u have an identifer it will be automaticly tokenized 32 and never ever need to be evaluated
about evaluate on %var is only if u have in same line another %var or if u r making a new identifer like

$ $+ %var 
or 
$ $+ $N

this requires an evaluation to get the results

 Respond  
Fuzionx   -  Dec 28, 2011

You're right, it also 'works' without the tokenize because the variables are evaluated somewhere else too.

 Respond  
WorldDMT   -  Dec 28, 2011

@Firstmate there are 3 variables which i based i saw only 4 entry maximum

@Fuzionx no need to evaluate $1- and no need to tokenize u can remove that line and see that's useless

 Respond  
Fuzionx   -  Dec 28, 2011

@WorldDMT I'm just saying the variables aren't evaluated if you don't use tokenize. What's so strange about that?

 Respond  
Firstmate   -  Dec 27, 2011

Yours is not...The OPs allows for any number of arguments, i.e. $vget(blah, test, foo, fah, words,46) Like I mentioned in my first post. Try your script with a variable of that nature and you get: "%blah.test is not set!"

 Respond  
WorldDMT   -  Dec 27, 2011

well, I just tested mine and it works well
and it gives the exact result of the OP

good luck

 Respond  
Firstmate   -  Dec 27, 2011

Well, either way, just like yours it wasn't fully tested. I just wanted to indicate your example was different the OP's snippet.

 Respond  
WorldDMT   -  Dec 27, 2011

Sorry for my accent because i don't speak English well

@Firstmate on "$vget(test,$me,0)" this will return %testSavage0 but the variable is without the "0" just %testSavage

and look at his code "if %name exist return %name else echo -ea %name is not set!" so when u use return to both the variable can be "Variable not set." and noting will be returned on active window

@Fuzionx wtf are you saying!! did you think twice before telling me this dumb story?

 Respond  
Fuzionx   -  Dec 27, 2011

@WorldDMT if he doesn't tokenize $($1-,2), the variables aren't evaluated and will just be plain text like: %var.

 Respond  
Firstmate   -  Dec 27, 2011

No idea. Not on my windows puter atm, but I'm sure I had a reason...I just can't remember it.

When does it not work? I didn't test it fully either.

 Respond  
WorldDMT   -  Dec 27, 2011

@Firstmate your code will not work 100%
and why did you tokenize $1- ???? it's already tokenized :/

 Respond  
Firstmate   -  Dec 27, 2011

Woops, it should be checking if $($+($,$0),2) is 0. Nice catch.

 Respond  
FelicianoX   -  Dec 27, 2011

Firstmate, no need to check if $0 isnum, it will always be a number, which is also why you don't need that $iif when setting %e.

 Respond  
Firstmate   -  Dec 26, 2011

Well his is organized so that you can supply any number of parameters, i.e. $vget(blah, test, foo, fah, words,46).
However, I agree in that a loop isn't necessary. Here was my take on it:

alias vget {
  tokenize 32 $($1-,2)
  var %d $iif($($+($,$0),2) != 0,$v1)
  var %e $($+(%,$replace($($iif(%d,$+($,1-,$calc($0 -1)),$+($,1-,$0)),2), $chr(32), $chr(%d))),2)
  if (%e) return %e
  else return Variable not set.
}

Though as you mentioned, you made yours for readability :V

 Respond  
WorldDMT   -  Dec 26, 2011

hi
i think no need to loop u can do it like this also

 alias vget {
  if (!$2) { echo -ea invalid parameters! | HALT }
  var %x $iif($0 = 4,$1-3,$1-2)
  if $(% $+ $replace(%x,$chr(32),$chr($(,$+($,$0)))),2) { return $v1 }
  else { echo 7 -ae % $+ $replace(%x,$chr(32),$chr($(,$+($,$0)))) is not set! }
}

P.S: this is just an E.G not tested yet only to give some idea

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.