Post Reply 
Change the way LOCAL variable declaration works.
06-08-2014, 05:28 PM
Post: #1
Change the way LOCAL variable declaration works.
Change the way LOCAL variable declaration works.
It is impossible to do indirect addressing totally within the scope of a program, independent from the outside. I've read HAN'S explanations for indirect addressing but what he suggests only works if the variable is already defined before runnig the prg. or as an input while prg. starts. What I need is this: I have an expression x+y+z and I want to do something to it. I do 'algvar' and get the list of variables {x,y,z}. I can sort the list, change its content to strings etc. and this gives me pointers, Han's explanation, and so far is OK. I want now to declare LOCAL VARIABLES based on the list {x,y,z} that 'algvar' gave me and now it does not work anymore because if vars={x,y,z} and I do LOCAL vars(1), vars(2), vars(3) it gives me error because x, y, z is not declared and this negates indirect addressing dependent only on the scope of my prg. I think that this is because no command works inside the scope of the LOCAL variable declaration, this is what I think but I could be wrong. It would be nice if LOCAL variable declaration would accept for example: LOCAL vars(1), vars(2),.., CAS("something"), EXPR("something"); where for example vars ={x,y,z}.
Find all posts by this user
Quote this message in a reply
06-08-2014, 06:45 PM
Post: #2
RE: Change the way LOCAL variable declaration works.
You can create CAS variables and clean them up afterward. Keep your variable names as strings, and initialize them through the CAS.

Code:

vars:={"a", "b", "c"};
tmp:=vars(1)+":=0";
CAS(tmp); // use a for loop to init all the vars if you want to init all of them

// do your stuff 

purge(vars(1));

You can also create a corresponding list of default values if you don't want to initialize to 0.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
06-08-2014, 07:11 PM
Post: #3
RE: Change the way LOCAL variable declaration works.
(06-08-2014 06:45 PM)Han Wrote:  You can create CAS variables and clean them up afterward. Keep your variable names as strings, and initialize them through the CAS.

Code:

vars:={"a", "b", "c"};
tmp:=vars(1)+":=0";
CAS(tmp); // use a for loop to init all the vars if you want to init all of them

// do your stuff 

purge(vars(1));

You can also create a corresponding list of default values if you don't want to initialize to 0.

Thank you for the help.
Find all posts by this user
Quote this message in a reply
06-10-2014, 06:01 AM
Post: #4
RE: Change the way LOCAL variable declaration works.
Hello,

Quote: [Kind of]What I am trying to do is take 'names' as input, declare local variables programatical based on these names, and then use the variables

Is that accurate?
If yes, then, sorry, but I can not think of any easy way to do it... As a mater of fact, very few comuter languages have such abilities!

One thing that pops in mind (might not work, but worth a try):

Get the list of variables that you are interested in.
Create a list with n elements (as many as you have variables).
Then REPLACE, in your original expression each of the variables names with 'list(n)' where n is the index in the list of the variable that you are replacing... and then it should work...

Now, please note that if you are mixing numerical and CAS stuff together, it will probably not work as numerica/CAS inverwork on variables is limited.

But this technique should work if you stay purely CAS or purely non CAS.
Take care of properly quoting the arguements that needs it.

Cyrille
Find all posts by this user
Quote this message in a reply
06-10-2014, 02:34 PM
Post: #5
RE: Change the way LOCAL variable declaration works.
(06-10-2014 06:01 AM)cyrille de brĂ©bisson Wrote:  Hello,

Quote: [Kind of]What I am trying to do is take 'names' as input, declare local variables programatical based on these names, and then use the variables

Is that accurate?
If yes, then, sorry, but I can not think of any easy way to do it... As a mater of fact, very few comuter languages have such abilities!

One thing that pops in mind (might not work, but worth a try):

Get the list of variables that you are interested in.
Create a list with n elements (as many as you have variables).
Then REPLACE, in your original expression each of the variables names with 'list(n)' where n is the index in the list of the variable that you are replacing... and then it should work...

Now, please note that if you are mixing numerical and CAS stuff together, it will probably not work as numerica/CAS inverwork on variables is limited.

But this technique should work if you stay purely CAS or purely non CAS.
Take care of properly quoting the arguements that needs it.

Cyrille

My problem was: I wanted to declare LOCAL variables from an expression as an argument to my prog. totaly independent of waring what the vars. in the expression were. I just wanted give to the program an expressin and some values corresopnding to the variables as an array and the program would evaluate and/or substitute those values in the expressin. Depending on the number of rows in the array of values I would get list of numbers and/or new expressins with something substituted for variables if I wanted to. Of course the values would have to have an order that would correspond to the variables in alphabetical order, the program would take care for sorting the extrakted variable. All should happen without me woring what the variables and in what order they were in the expression and everything would happen in the scope of the program so at the end I would get the list of values/expressins and no garbage left outside of the program. To do that I needed to declare the extracted variables from the expressin as a LOCAL variables and that did not work because whenever I took a variable from the variables list and try to do it as LOCAL I was getting an error.

I wanted to suggest, in my post, to make the LOCAL declaratin more flexible so it would accept and declare as locals variables from some list witout an error ex. LOCAL vars(1), vars(2) etc. LOCAL accepts for example x or x={} etc why it can not accept for example vars(1) and take whatever vars(1) is as long as it evaluates to a variable. That should not be to difficult to impliment, I think.

I solved the problem using Han's explanation for creating CAS variables but they are stored as global vars but that is not a big problem because at the end I can purge them. The program I wrote works quite well and is olomost 98% what I wanted. I would also like to mention thet the command in HP Prime 'algvar' is not 100% perfect. It works as expected when I use 'algvar(z+x+y)' and gives [[z,x,y]] but when the expression is SIN(z)+y+x 'algvar(SIN(z)+y+x)' gives as variables [[SIN(z),y,x]] and that is not correct because SIN(z) is a funktion not a variable z is the variable.

Thank you Cirill for your time to answare my post, much appreciated.
Find all posts by this user
Quote this message in a reply
06-10-2014, 03:21 PM
Post: #6
RE: Change the way LOCAL variable declaration works.
(06-10-2014 02:34 PM)John P Wrote:  wanted. I would also like to mention thet the command in HP Prime 'algvar' is not 100% perfect.

Yes, it is (as far as I can tell). It just isn't doing what you think it is doing! Smile

lname is the command you are looking for here. There are some slight differences betweeen lname, lvar and algvar that allow them to be used for different things.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
06-10-2014, 04:09 PM
Post: #7
RE: Change the way LOCAL variable declaration works.
(06-10-2014 03:21 PM)Tim Wessman Wrote:  
(06-10-2014 02:34 PM)John P Wrote:  wanted. I would also like to mention thet the command in HP Prime 'algvar' is not 100% perfect.

Yes, it is (as far as I can tell). It just isn't doing what you think it is doing! Smile

lname is the command you are looking for here. There are some slight differences betweeen lname, lvar and algvar that allow them to be used for different things.

Thank you Tim.
Find all posts by this user
Quote this message in a reply
Post Reply 




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