Dynamic local variables
03-13-2015, 07:59 PM
Post: #1 Han Senior Member Posts: 1,863 Joined: Dec 2013
Dynamic local variables
Here's an example of how to create dynamic local variables:

Code:
locvars:={}; locname:={}; NEWLOC(); LSTO(); LRCL(); LSTON(); LRCLN(); EXPORT DYNLOC() BEGIN   // an example   local mylocal;   mylocal:=1;   NEWLOC("cool",0);   LSTO("cool",-5);   NEWLOC("alist",1);   LSTO("alist",{1,2,3});   mylocal:=LRCL("cool")*3;   LRCL("alist")*mylocal+LRCLN("alist",1); END; // name is a string // vtype = 0 --> atomic;  NEWLOC(name,vtype) BEGIN   local j;   j:=POS(locname,name);    if vtype then     locvars(j):={0};   else     locvars(j):=0;   end;   locname(j):=name;  END; // name is a string // val is the value to store // n is the index (0 if atomic)  LSTON(name,val,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index: " + name + "(" + n + ")");         kill;       end;       locvars(j,n):=val;     else       locvars(j):=val;     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; // name is a string // n is the index (0 if atomic) LRCLN(name,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index " + name + "(" + n + ")");         kill;       end;       return(locvars(j,n));     else       return(locvars(j));     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LSTO(name,val) BEGIN   local j;   j:=POS(locname,name);   if j then     locvars(j):=val;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LRCL(name) BEGIN   local j;   j:=POS(locname,name);   if j then     return(locvars(j));   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END;

Explanation to come a bit later; heading home from work.

Graph 3D | QPI | SolveSys
03-14-2015, 10:56 AM
Post: #2 salvomic Senior Member Posts: 1,392 Joined: Jan 2015
RE: Dynamic local variables
thanks Han,
another bookmark for my "study soon the Prime programming" folder ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
03-15-2015, 07:44 PM
Post: #3 Han Senior Member Posts: 1,863 Joined: Dec 2013
RE: Dynamic local variables
(03-13-2015 07:59 PM)Han Wrote:  Here's an example of how to create dynamic local variables:

Code:
locvars:={}; locname:={}; NEWLOC(); LSTO(); LRCL(); LSTON(); LRCLN(); EXPORT DYNLOC() BEGIN   // an example   local mylocal;   mylocal:=1;   NEWLOC("cool",0);   LSTO("cool",-5);   NEWLOC("alist",1);   LSTO("alist",{1,2,3});   mylocal:=LRCL("cool")*3;   LRCL("alist")*mylocal+LRCLN("alist",1); END; // name is a string // vtype = 0 --> atomic;  NEWLOC(name,vtype) BEGIN   local j;   j:=POS(locname,name);    if vtype then     locvars(j):={0};   else     locvars(j):=0;   end;   locname(j):=name;  END; // name is a string // val is the value to store // n is the index (0 if atomic)  LSTON(name,val,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index: " + name + "(" + n + ")");         kill;       end;       locvars(j,n):=val;     else       locvars(j):=val;     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; // name is a string // n is the index (0 if atomic) LRCLN(name,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index " + name + "(" + n + ")");         kill;       end;       return(locvars(j,n));     else       return(locvars(j));     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LSTO(name,val) BEGIN   local j;   j:=POS(locname,name);   if j then     locvars(j):=val;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LRCL(name) BEGIN   local j;   j:=POS(locname,name);   if j then     return(locvars(j));   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END;

Explanation to come a bit later; heading home from work.

And for the explanation:

Normally, one must declare all variables prior to their use. In this template, one only has to declare two generic container variables: localvars and localname. The variable localvars stores the values of the variables whereas localname stores their ascii names. We use localname as a lookup table to determine the index to use within localvars get the corresponding value.

NEWLOC("varname", type) -- creates a variable with name "varname" and initializes it to either 0 or {0} depending on whether type is 0 or non-zero (atomic versus composite variable). Thus, to create a list, one could do: NEWLOC("mylist",1). If that variable already exists, then it is reset. If not, then it is created as a new entry in both localvars and localname.

LSTO("varname",value) -- stores a value into "varname"

LSTON("varname", value, index) -- stores a value into the n-th position in a composite variable "varname". For example, LSTON("mylist",-3,5) is equivalent to mylist(5):=-3

LRCL and LRCLN behave similarly but recalls the value stored in the local variables.

Graph 3D | QPI | SolveSys
05-19-2019, 08:35 PM
Post: #4
 tom234 Member Posts: 75 Joined: Aug 2016
RE: Dynamic local variables
(03-13-2015 07:59 PM)Han Wrote:  Here's an example of how to create dynamic local variables:

Code:
locvars:={}; locname:={}; NEWLOC(); LSTO(); LRCL(); LSTON(); LRCLN(); EXPORT DYNLOC() BEGIN   // an example   local mylocal;   mylocal:=1;   NEWLOC("cool",0);   LSTO("cool",-5);   NEWLOC("alist",1);   LSTO("alist",{1,2,3});   mylocal:=LRCL("cool")*3;   LRCL("alist")*mylocal+LRCLN("alist",1); END; // name is a string // vtype = 0 --> atomic;  NEWLOC(name,vtype) BEGIN   local j;   j:=POS(locname,name);    if vtype then     locvars(j):={0};   else     locvars(j):=0;   end;   locname(j):=name;  END; // name is a string // val is the value to store // n is the index (0 if atomic)  LSTON(name,val,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index: " + name + "(" + n + ")");         kill;       end;       locvars(j,n):=val;     else       locvars(j):=val;     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; // name is a string // n is the index (0 if atomic) LRCLN(name,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index " + name + "(" + n + ")");         kill;       end;       return(locvars(j,n));     else       return(locvars(j));     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LSTO(name,val) BEGIN   local j;   j:=POS(locname,name);   if j then     locvars(j):=val;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LRCL(name) BEGIN   local j;   j:=POS(locname,name);   if j then     return(locvars(j));   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END;

Explanation to come a bit later; heading home from work.

(03-15-2015 07:44 PM)Han Wrote:
(03-13-2015 07:59 PM)Han Wrote:  Here's an example of how to create dynamic local variables:

Code:
locvars:={}; locname:={}; NEWLOC(); LSTO(); LRCL(); LSTON(); LRCLN(); EXPORT DYNLOC() BEGIN   // an example   local mylocal;   mylocal:=1;   NEWLOC("cool",0);   LSTO("cool",-5);   NEWLOC("alist",1);   LSTO("alist",{1,2,3});   mylocal:=LRCL("cool")*3;   LRCL("alist")*mylocal+LRCLN("alist",1); END; // name is a string // vtype = 0 --> atomic;  NEWLOC(name,vtype) BEGIN   local j;   j:=POS(locname,name);    if vtype then     locvars(j):={0};   else     locvars(j):=0;   end;   locname(j):=name;  END; // name is a string // val is the value to store // n is the index (0 if atomic)  LSTON(name,val,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index: " + name + "(" + n + ")");         kill;       end;       locvars(j,n):=val;     else       locvars(j):=val;     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; // name is a string // n is the index (0 if atomic) LRCLN(name,n) BEGIN   local j;   j:=POS(locname,name);   if j then     if n then       if n>size(locvars(j)) then         MSGBOX("Invalid index " + name + "(" + n + ")");         kill;       end;       return(locvars(j,n));     else       return(locvars(j));     end;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LSTO(name,val) BEGIN   local j;   j:=POS(locname,name);   if j then     locvars(j):=val;   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END; LRCL(name) BEGIN   local j;   j:=POS(locname,name);   if j then     return(locvars(j));   else     MSGBOX("Error: no such local var (" + name + ")");     kill;   end; END;

Explanation to come a bit later; heading home from work.

And for the explanation:

Normally, one must declare all variables prior to their use. In this template, one only has to declare two generic container variables: localvars and localname. The variable localvars stores the values of the variables whereas localname stores their ascii names. We use localname as a lookup table to determine the index to use within localvars get the corresponding value.

NEWLOC("varname", type) -- creates a variable with name "varname" and initializes it to either 0 or {0} depending on whether type is 0 or non-zero (atomic versus composite variable). Thus, to create a list, one could do: NEWLOC("mylist",1). If that variable already exists, then it is reset. If not, then it is created as a new entry in both localvars and localname.

LSTO("varname",value) -- stores a value into "varname"

LSTON("varname", value, index) -- stores a value into the n-th position in a composite variable "varname". For example, LSTON("mylist",-3,5) is equivalent to mylist(5):=-3

LRCL and LRCLN behave similarly but recalls the value stored in the local variables.

////
this does not run on HPG2 why.
05-20-2019, 04:52 AM
Post: #5 Han Senior Member Posts: 1,863 Joined: Dec 2013
RE: Dynamic local variables
(05-19-2019 08:35 PM)tom234 Wrote:
(03-13-2015 07:59 PM)Han Wrote:  Here's an example of how to create dynamic local variables:

(code removed for legibility)

Explanation to come a bit later; heading home from work.

(03-15-2015 07:44 PM)Han Wrote:  And for the explanation:

Normally, one must declare all variables prior to their use. In this template, one only has to declare two generic container variables: localvars and localname. The variable localvars stores the values of the variables whereas localname stores their ascii names. We use localname as a lookup table to determine the index to use within localvars get the corresponding value.

NEWLOC("varname", type) -- creates a variable with name "varname" and initializes it to either 0 or {0} depending on whether type is 0 or non-zero (atomic versus composite variable). Thus, to create a list, one could do: NEWLOC("mylist",1). If that variable already exists, then it is reset. If not, then it is created as a new entry in both localvars and localname.

LSTO("varname",value) -- stores a value into "varname"

LSTON("varname", value, index) -- stores a value into the n-th position in a composite variable "varname". For example, LSTON("mylist",-3,5) is equivalent to mylist(5):=-3

LRCL and LRCLN behave similarly but recalls the value stored in the local variables.

////
this does not run on HPG2 why.

What issues did you run into? Were there any error messages at all?

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)