Page 1 of 1

Set Multiple Vars

PostPosted: Mon Dec 17, 2012 1:38 pm
by dpayer
You can set multiple variables with one line of code with this function.

Syntax:

Code: Select all
  CALL "SetMultiVar" "var1=value1;var2=value2;var3=value3;var4=value4"


Note: no error correction done if your strings contains additional equal signs, quote marks or semi colons besides those used for delimiters.


Save the following code in your Functions folder (under NB)
Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
Comment=Note: no error correction done if your strings contain equal signs, quote marks or semi colons. |
Param=[%1]|Text|String with varname=value entries separated by semicolon
{End}
SearchStr ";" "[%1]" "[MultiVarArrayDelimiterFound]" ""
If "[MultiVarArrayDelimiterFound]" ">" "0"
StrParse "[%1]" ";" "[MultivarArray]" "[MultivarArrayLen]"

  Loop "1" "[MultivarArrayLen]" "[MultivarWILL]"
     SearchStr "=" "[MultivarArray[MultivarWILL]]" "[MultiVarArrayEqualFound]" ""
     If "[MultivarArrayEqualFound]" ">" "0"
       StrParse "[MultivarArray[MultivarWILL]]" "=" "[MultivarElement]" ""
       SetVar "[[MultivarElement1]]" "[MultivarElement2]"
     Else
     Alertbox "Error" "No equal delimiter found in string"
     Endif
  Endloop
Else
  Alertbox "Error" "No delimiter found in string"
Endif



Sample pub - using function named as SetMultiVar
(copy content below and paste into blank NB page):
Code: Select all
{NeoBook 5 Objects}
NeoBookVer=5.70
ObjectType=3
Name=PushButton1
X=24
Y=136
W=280
H=40
Anchor=0
Text=Call MultiVar Set Function
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=Call "SetMultiVar" "var1=1;var2=2;var3=3;var4=4;varNOT5=not5"¶¶AlertBox "test" "Var1 = [Var1]|Var2 = [Var2]|Var3 = [Var3]|Var4 = [Var4]|VarNOT5 = [VarNOT5]"
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2

Re: Set Multiple Vars

PostPosted: Sun Dec 23, 2012 7:05 pm
by smokinbanger
Nice, Thanks!

Re: Set Multiple Vars

PostPosted: Sun Dec 23, 2012 7:54 pm
by Gaev
DavidP:

1) suggested enhancement ...

//specify primary separator string
CALL "SetMultiVar" "$@;" "var1=value1$@;var2=value2$@;var3=value3$@;var4=value4"
//same for secondary separator string
CALL "SetMultiVar" "$@;" "=_=" "var1=_=value1$@;var2=_=value2$@;var3=_=value3$@;var4=_=value4"

2) BTW, f.y.i. this can also be achieved with pure NeoBook commands ... key is to exploit NeoBook's nested parsing of brackets e.g. ...
Code: Select all
SetVar "[abc]" "var1"
SetVar "[[abc]] "[someValue]"
... will set variable [var1] to value in variable [someValue]