Page 1 of 1

Assigning value of a variable to another with programming flow control

Posted: Fri May 14, 2021 4:34 pm
by parag_patil
Hello,

I want to assign the existing values of variables to another variables using either of the following:
  • loop statement
  • for statement
  • if-else statement Or
  • while statement
I do not want to use conditional ($) statements, because the number of elements is large

Code: Select all

sets
tt /tt1*tt5/
t /t1*t10/
;

Variables

X(tt) , Y(time);
X.fx('tt1') = 1;
X.fx('tt2') = 2;
X.fx('tt3') = 3;
X.fx('tt4') = 4;
X.fx('tt5') = 5;


Y.fx('t1') = X.l('tt1');
Y.fx('t2') = X.l('tt1');
Y.fx('t3') = X.l('tt2');
Y.fx('t4') = X.l('tt2');
Y.fx('t5') = X.l('tt3');
Y.fx('t6') = X.l('tt3');
Y.fx('t7') = X.l('tt4');
Y.fx('t8') = X.l('tt4');
Y.fx('t9') = X.l('tt5');
Y.fx('t10') = X.l('tt5');

Thanks

Re: Assigning value of a variable to another with programming flow control

Posted: Sun May 16, 2021 7:05 am
by bussieck
Use parallel assignment statement and the power of sets where ever possible and avoid regular programming constructs. Here your program how it ought to be written:

Code: Select all

sets tt /tt1*tt5/, t /t1*t10/;
Variables X(tt) , Y(t);

X.fx(tt) = ord(tt);
set tmap(t,tt); tmap(t,tt) = ceil(ord(t)/2)=ord(tt);
Y.fx(t) = sum(tmap(t,tt), X.l(tt));
-Michael

Re: Assigning value of a variable to another with programming flow control

Posted: Mon May 17, 2021 9:14 am
by parag_patil
Thanks.

The answer was helpful.