hp prime - programming with lists



#5

Hello,
Today i was playing with lists in programs and in particular list of strings. Please tell me if i am wrong with explorations.

1) sort(list) doesn't work with strings. Items are unchanged after 'sort'. 'Sort' does work with numbers.

2) i am not able to delete an item of a list in my program. I was trying to remove a string from a list in vain. Since i wasn't able to remove it, i assigned an empty value "" to it. Do you know which command there is to remove an item from a list? I could write a program to do it in case it doesn't exist.

3) i read somewhere in this forum that the way to assign a string to a variable via a Input() command was to define the variable and assign a value to it like:
local X2:="";
input (x2, "my title","my variable");
The result is not very useful. I can see an empty string into the variable entry field but i cannot use it because i edit the command line so i have to manually add the string "" and then entry my text.

4) I defined an input() of a string in a "repeat... until..." loop. When i run into this loop the first time i can entry the string but starting from the second loop, when i see the input screen of the string, i can still see the value i entered during the first run. Did you encouter the same issue?

Thanks

Giancarlo

P.s. please answers to my points referring to the point numbers i indicated. The reading will be easier.


#6

ad 2: remove(what,list) removes the item 'what' from a list. To be precise, 'what' is either a boolean function to identify list items to remove or the value to remove. 'what' is not the position! A workaround could be to replace the item in question by a special value and then use remove() to remove all occurrences of the marker from the list.

#7

1+2) Here is a couple utilities about list handling in a programming point of view. The name come from the language Clipper (xBase)

with examples at the end

EXPORT AAdd(Lst, Val_)  // add element at end of list
BEGIN
RETURN CONCAT(Lst, {Val_});
END;

EXPORT AIns(Lst, Pos_, Val_) // insert element in list
BEGIN
IF Pos_ == 1 THEN RETURN CONCAT({Val_}, Lst); END;
IF Pos_ > SIZE(Lst) THEN RETURN CONCAT(Lst, {Val_}); END;
RETURN CONCAT(CONCAT(Sub(Lst, 1, Pos_-1), {Val_}), Sub(Lst, Pos_, (SIZE(Lst))));
END;

EXPORT ADel(Lst, Pos_) // delete element of list
BEGIN
LOCAL Tmp;
IF Pos_ == 1 THEN
IF SIZE(Lst) == 1 THEN Tmp:= {};
ELSE Tmp:= SUB(Lst,2,SIZE(Lst)); END;
ELSE
IF Pos_ == SIZE(Lst) THEN Tmp:= SUB(Lst,1,SIZE(Lst)-1);
ELSE Tmp:= CONCAT(Sub(Lst, 1, Pos_-1), Sub(Lst, Pos_+1, (SIZE(Lst))));
END;
END;
RETURN Tmp;
END;

EXPORT ADelTail(Lst) // delete last element of list
BEGIN
IF SIZE(Lst) == 1 THEN RETURN {};
ELSE RETURN SUB(Lst,1,SIZE(Lst)-1); END;
END;

EXPORT ASize(Lst, NewSize) // resize list
BEGIN
LOCAL Tmp;
IF NewSize == 0 THEN Tmp:= {}; END;
IF SIZE(Lst) > NewSize THEN Tmp:= SUB(Lst,1, NewSize); END;
IF SIZE(Lst) == NewSize THEN Tmp:= Lst; END;
IF SIZE(Lst) < NewSize THEN Tmp:= CONCAT(Lst, MAKELIST(0, A, SIZE(Lst)+1, NewSize)); END;
RETURN Tmp;
END;

EXPORT ATail(Lst) // Last element of list
BEGIN
RETURN Lst(SIZE(Lst));
END;

EXPORT ASort(Lst, Col) // Sort a list of lists
BEGIN
LOCAL Scan, Tmp, Pos, Db, Fn, Ml;
IF SIZE(Lst) > 0 THEN
Tmp:= {Lst(1)};
FOR Scan FROM 2 TO SIZE(Lst) DO
IF Lst(Scan,Col) < Tmp(1,Col) THEN Tmp:= AIns(Tmp, 1, Lst(Scan)); ELSE
IF Lst(Scan,Col) >= Tmp(SIZE(Tmp),Col) THEN Tmp:= AAdd(Tmp, Lst(Scan)); ELSE
Db:= 2; Fn:= SIZE(Tmp)-1;
WHILE Db <= Fn DO
Ml:= IP((Db+Fn)/2);
IF Tmp(Ml,Col) <= Lst(Scan,Col) THEN Db:= Ml+1;
ELSE Fn:= Ml-1; END;
END;
Tmp:= AIns(Tmp, Db, Lst(Scan));
END;
END;
END;
ELSE
Tmp:= {};
END;
RETURN Tmp;
END;

EXPORT TestList()
BEGIN
LOCAL Cnt, List;
List:= {};
FOR Cnt FROM 1 TO 15 DO
List:= AAdd(List, Cnt);
MSGBOX(ATail(List)+":"+STRING(List));
END;

List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 15 DO
List:= ADel(List, 1);
MSGBOX(List);
END;

List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 10 DO
List:= ADel(List, 3);
MSGBOX(List);
END;

List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 15 DO
List:= ADelTail(List);
MSGBOX(List);
END;

END;


#8

Hello Markus and Patrice,
Thank you very much for your answer. I am goign to work on this today and let you know the result(s).

Regarding point 4). Running a loop with an input() command in it is obvious that the local variable maintain the last value entered at each iteration of the loop. Anyway i solved the issue, assigning a "" value at the end of each iteration.

At this point only question 3 remains unsolved. Any idea on how to input() a string in a clean way? This is what i tried:

EXPORT PROVA()
BEGIN

LOCAL X1:="";
INPUT(X1,"titolo","stringa: ");

END

As said at the first post of this thread, i assigned to my local variable an empty string "". When i execute the program, i can see the two "" in the variable entry field at the top of the screen but i use the command line to fill my data and i have to write my string in brackets again.
At this point there is no need to preassign a string if you can't use its predefined brakets ("").

Thanks for any hint in this field.

Giancarlo from cappuccino state


Forum Jump: