Post Reply 
{ Vars List} how to store a value in one var of the list ?
02-05-2014, 11:01 AM
Post: #1
{ Vars List} how to store a value in one var of the list ?
Hi Friends,

Inside a program, {'A1','A2','A3'} is created dynamically, then stored in L1

L1:={'A1','A2','A3'};

I need to store a value, for example 4 in 'A1'...
L1(1) does reference to 1st position in the list so L1(1):=4;
generates {4,'A2','A3'} and It is not desired ... I want to do A1:=4; for future use in the program.

So...How to store (if possible like HP 50g RPN: 4 L1 1 GET STO) a value in one list´s variable ??

PD. I don´t want to define...LOCAL A1, A2, A3.....An

Thanks and best regards!!
joseph
Find all posts by this user
Quote this message in a reply
02-05-2014, 12:42 PM (This post was last modified: 02-05-2014 12:52 PM by Snorre.)
Post: #2
RE: { Vars List} how to store a value in one var of the list ?
Hello Joseph,

You could try an EXPR("A1:="+value); or in your case EXPR(L1(k)+":="+value);
But I think this won't work in your program since all program variables have to be previously defined, even if used in such a "dynamic" context.
If you want your values to be accessible in CAS you may do a CAS(L1(k)+":="+value) as the CAS can create variables on demand.

Why is it so important to store your values in distinct variables?
You could also store them in a second list (global within your program but invisible to the outside world) and use your L1 just as an index map, e.g.

--------8<---------
storage:=MAKELIST(0,I,1,n); // n actual values (preserved between program calls)

MyProg()
BEGIN
L1:={index1,index2,...}; // indirection table
// ... do something ...
storage(L1(k)):=value; // store value at the L1(k)'th register
END;
-------->8---------
Find all posts by this user
Quote this message in a reply
02-05-2014, 02:46 PM
Post: #3
RE: { Vars List} how to store a value in one var of the list ?
(02-05-2014 12:42 PM)Snorre Wrote:  You could try an EXPR("A1:="+value); or in your case EXPR(L1(k)+":="+value);
ohhh YES!!!, that works perfect... even I already used the powerfull EXPR command to call programs with arguments.
Quote:Why is it so important to store your values in distinct variables?

Well is a program to solve Differential Equations, n equations really, may be 2, 3 o more that user enter in a list {Fj(T, U1, U2.. Un)} an example for n=3 {'U2+U1*T', 'T+U3', 'U2+U3'} so.. dynamically I create variables {'U1','U2', U3'} then I store some values in Uj and evaluate in the approx(Fj)... it could be solver declaring U1... U10
that is enough but a of powerfull calculator and good program are done to manage n equations/variables with no limits.

Thanks so much for you help... yesterday i tried EXPR("L(i):=...") and really i need to separate L(i).

Best regards!!
jose
Find all posts by this user
Quote this message in a reply
02-05-2014, 03:06 PM
Post: #4
RE: { Vars List} how to store a value in one var of the list ?
What about

#{"V1","V2","V3"}(2):=88

?

This is very easy...

Best greetings OldHPUser
Find all posts by this user
Quote this message in a reply
02-06-2014, 02:07 PM
Post: #5
RE: { Vars List} how to store a value in one var of the list ?
(02-05-2014 03:06 PM)OldHPUser Wrote:  What about

#{"V1","V2","V3"}(2):=88

?

This is very easy...

Best greetings OldHPUser

OK, but there are only 3 vars, and if user enter more eqs we will need more vars.

The best solutions is create a list of vars dynamic (for n eqs entered by user):
LOCAL uj;
uj:={};
FOR I FROM 1 TO n DO
uj(I):=EXPR("'U"+I+"'");
END;

To store values we only need to do
EXPR(uj(index)+":="+value);

now You can evaluate eq var that has stored algebraic expression like 'U1+U2-..T*U5'
AnyVar:=approx(eq)

Best regards!!
jose
Find all posts by this user
Quote this message in a reply
02-07-2014, 11:55 AM
Post: #6
RE: { Vars List} how to store a value in one var of the list ?
Hi,
of couse there are several ways for doing the job!

#{str1,str2,...}(n):=value works with an arbitrary number of parameters, of course (up to 10.000 parameters in this release of the HP Prime). str-i should contain the name of the variable-i.

Moreover, you can define an universal XCAS function, which does the work for You erverytime:

VarsSetUp(n,V,L):=BEGIN LOCAL u01; u01:=L(n); #(u01):=V; END;
(Input from the CAS command line)

Best Greetings
OldHPUser
Find all posts by this user
Quote this message in a reply
Post Reply 




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