HP Forums
Cleaning up code - help me - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Cleaning up code - help me (/thread-5338.html)



Cleaning up code - help me - chromos - 12-15-2015 05:57 AM

Hi,
I have static INPUT code, which looks like this:
Code:
EXPORT testinput()
BEGIN
INPUT({{L3(1),[0],{40,10,1}},{L4(1),[0],{60,10,1}},
{L3(2),[0],{40,10,2}},{L4(2),[0],{60,10,2}}},
"Variable domains",
{"'A' is FROM ","TO ",
"'B' is FROM ","TO "},
{"Lower limit for 'A'",
"Upper limit for 'A'",
"Lower limit for 'B'",
"Upper limit for 'B'"});
END;

But number of fields is dependent on previous user's input, so I wrote this:
Code:
Loop;
Temp;
Temp1;
Temp2;
EXPORT testinput2()
BEGIN
Temp:="INPUT({";
Temp1:="";
Temp2:="";
FOR Loop FROM 1 TO SIZE(L2) DO
 Temp:=Temp+"{L3("+Loop+"),[0],{40,10,"+Loop+"}},{L4("+Loop+"),[0],{60,10,"+Loop+"}}";
 IFTE(Loop<SIZE(L2),Temp:=Temp+",",Temp:=Temp+"}");
 Temp1:=Temp1+STRING("'"+L2(Loop)+"' is FROM ")+","+STRING("TO ");
 IFTE(Loop<SIZE(L2),Temp1:=Temp1+",",Temp1:=Temp1+"},");
 Temp2:=Temp2+STRING("Lower limit for '"+L2(Loop)+"'")+","+STRING("Upper limit for '"+L2(Loop)+"'");
 IFTE(Loop<SIZE(L2),Temp2:=Temp2+",",Temp2:=Temp2+"})");
END;
Temp1:="{"+Temp1;
Temp2:="{"+Temp2;
Temp:=Temp+","+STRING("Variable domains")+","+Temp1+Temp2;
EXPR(Temp);
END;

Please, is there other way how to make dynamic input than this almost unreadable code?
[attachment=2958]


RE: Cleaning up code - help me - cyrille de brébisson - 12-16-2015 07:44 AM

Hello,

For something like this, the code will be somewhat hard to read.

Even the mighty MAKELIST can not help you here as you are trying to generate 2 elements per loop.

I did try to simplify things using lists, but even this fails because you need to do something like:
List(0):= { L3(Varaible), ....}
with Variable being evaluated but L3(...) being NOT evaluated!
I guess if PPL had an apply function, it could be build, but even that would not be very obvious/readable.

Cyrille


RE: Cleaning up code - help me - Didier Lachieze - 12-16-2015 05:20 PM

Here is what I've been able to achieve using MAKELIST, working but quite ugly:

Code:
EXPORT testinput2()
BEGIN
LOCAL InputList:={}, Temp, s:=2*SIZE(L2)+1;
  InputList(1):= MAKELIST(EXPR("'{'L"+(3+odd(X))+"("+IP(X/2)+ ")',[0],{"+(4+2*odd(X))+"0,10,"+ IP(X/2)+ "}}'"),X,2,s);
  InputList(2):= "Variable domains";
  InputList(3):= MAKELIST(IFTE(even(X),"'"+L2(X/2)+ "' is FROM " , "TO "),X,2,s);
  InputList(4):= MAKELIST(IFTE(even(X),"Lower limit for '"," Upper  limit for '")+L2(IP(X/2))+ "'" ,X,2,s);
  Temp:=STRING(InputList); Temp(1):= "("; Temp(DIM(Temp)):= ")";
  EXPR("INPUT"+Temp);
END;



RE: Cleaning up code - help me - chromos - 12-17-2015 06:25 AM

Thank you, Cyrille and Didier, for your input. It seems I have personal assistants now. Sorry if my questions are dumb so you waste your precious time on them. :-)


RE: Cleaning up code - help me - cyrille de brébisson - 12-17-2015 07:23 AM

Hello,

No, not dumb, they are interesting and show what people want and manage to achieve with PPL.

Have fun!

Cyrille