(50G) Ver8. Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (50G) Ver8. Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE (/thread-22548.html) |
(50G) Ver8. Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE - Gil - 10-21-2024 07:14 PM As I don't use libraries and had to use lists, I created here 6 basic small programs contained in a directory called LIST. The original list and transformed list accepts now any kind of objects (strings, numbers, variables, embedded lists...). L.SWAP With always original list {Elem1 Elem2...ElemN} at upper stack level Switches the position of the elements given in last 2 stacks (or the position of the 2 elements contained in the list in stack level 1). L.DEL With always original list {Elem1 Elem2...ElemN} at upper stack level Deletes, from the given position in stack level 2 to other position in stack level 1, all the corresponding elements (or, if a list of positions in the stack level 1 right after the original list in stack level 2, it deletes the corresponding elements of the original list). Special case: suppose that you want to delete specific elements (the positions of which you don't know), then use R.eRPL below instead, putting the element to be deleted (or the elements to be deleted as a list) in stack level 2, and "" (an empty string) (or a list of "") in stack level 1 and R.eRPL. Example to suppress {20 50 } of the following list in the stack: { 10 20 30 40 50 } { 20 50 } { "" "" } R.eRPL gives { 10 30 40 } :Changes: 2 } L.KEEP With always original list {Elem1 Elem2...ElemN} at upper stack level Keeps, from the given position in stack level 2 to other position in stack level 1, all the corresponding elements (or, if a list of positions in the stack level 1 right after the original list on stack level 2, it keeps the corresponding elements of that list). L.ADD With always original list {Elem1 Elem2...ElemN} at upper stack level Adds, at a given position in the stack 2, the element given in stack level 1 (or the given elements contained in the list in stack level 1). L.RPL when dealing initially with position With always original list {Elem1 Elem2...ElemN} at upper stack level Replaces, for a given position at stack level 2 (or for the given positions contained in a list at stack 2), the corresponding value (or values) by the value in the last stack (or by the values of the list in the last stack). It might deletes, in one step, many positions in a sequence, from position.i (given in stack level 3) up to position.k (given in stack level 2), all the corresponding elements being then replaced by element (elements in a list) in stack level 1. Further explanations in next post/reply. LeRPL when dealing initially directly with specific elements/objects With always original list {Elem1 Elem2...ElemN} at upper stack level Replaces given specific elements contained in a list in stack level 2 by the corresponding new elements contained in the list in the last stack. The list in stack level 2 might start with an optional special element: ∞# (infinity sign and a number). Example {∞2 a b}, which means that each of the two element a & b should be replaced maximum 2 times (maximum 2 times for a and also maximum 2 times for b). RE: 50G Lists Manipulation - Gil - 10-23-2024 01:36 PM Version 8 Replacements, given positions, with L.RPL examplified below by the 3 cases A), B) & C). Replacements, given specific elements, with LeRPL examplified below by the 5 cases D) to H). A) Suppose that, having the following initial list {10 20 30 40 50 60 70} in the stack level 1, you want to suppress the element #4 up to element #6 (ie suppress the 3 elements 40, 50 & 60) & have at their place the single element 1000, then do 4 6 1000 L.RPL, to get finally {10 20 30 1000 70}. B) Almost same example as in A), suppose that now you want to get, instead of the new single element 1000, two new elements: 1000 & 2000. Then, having again the initial list {10 20 30 40 50 60 70} in the stack level 1, use a list as follows: do 4 6 {1000 2000} L.RPL, to get finally {10 20 30 1000 2000 70}. C) Suppose now, having always the same initial list {10 20 30 40 50 60 70} in the stack level 1, that you want to change 2 specific éléments of that list, let's say element #3 & element #5, by their corresponding new elements, say respectively 300 & 500, then use lists twice as follows: {3 5} {300 500} L.RP, to get finally {10 20 300 40 500 60 70}. Observations 1) If list in stack level 2 is of dimension k>1, then there must be — logically — a corresponding list of the same dimension k in stack level 1. Example not logical (but however accepted) : {10 20 30 40} { 2 3} {200}, as the last 2 lists are not of the same size, {2 3} being of dimension 2, whereas the last list {200} is of dimension 1. The 2nd list {2 3} specifies that its 1st element, ie 2—>20 in the original list, is to be replaced by the 1st element (200) of the 3rd list {200}, and that's OK; but the 2nd list {2 3} specifies also that its 2nd element 3—>30 in the original list should be replaced by the 2nd element of the 3rd list {200}, element that is not defined here. Note that, in that specific non logical case, the program will not produce an error, giving as a final result {10, 200, 30, 40}, leaving the element#3, 30, untouched; however an error will occur if, instead of the list {200} in stack level 1, we had just 200 (as 200 is not a list). 2) If, however, the objet in stack level 2 is a single number (position) or a list of dimension 1 (k=1), then the object in stack level 1can be a list of dimension k>=1 (k=1, k=2, etc.) or a non-list single element. Examples allowed: {10 20 30 40} 3 {31 32} S.RPL or, alternatively, {10 20 30 40} {3} {31 32} S.RPL —> both ways of writing (3 or {3}) are strictly equivalent and suppress the 3rd element 30 of the initial list & replace it by 31 and 32, which gives {10 20 31 32 40}. Case D) with LeRPL {10 20 30 40 50 20 30 20 30 } {20 30} {200 300} LeRPL gives {10 200 300 40 50 200 300 200 300 } Changes: 6 Case E) with LeRPL and maximum of 2 replacements for each element {10 20 30 40 50 20 30 20 30 } {∞2 20 30} {200 300} LeRPL gives {10 200 300 40 50 200 300 20 30 } Changes: 4 (the 3rd occurrence of 20 & 30 is not taken into account). Note: for maximum of replacements M for each element, add always the element ∞M at the beginning of the list in stack level 2. Case F with LeRPL { "A" 10 20 30 "B" } { 10 20 30 } { 100 "" 300 } LeRPL gives { "A" 100 300 "B" } Changes: 3 The double quotation marks, with nothing inside (empty string), are meant to delete the element 20 and then not to replace it. Note: the "indicator" to delete an element (here the quotation marks as an empty string) is saved in local variable d (at the beginning of the program), whose content can be changed : instead of "", you could choose and put, for the local variable d , "0" "?" "??" '?' ??', for instance. Case G with LeRPL { 10 20 30 40 20} 20 "" LeRPL gives { 10 30 40 } :Changes: 2 Case H with LeRPL { 10 20 { 30 } 40 50 } { { 30 } 50 } { { A } { B } } LeRPL gives { 10 20 { A } 40 { B } } :Changes: 2 RE: (50G) Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE - Gil - 11-10-2024 01:57 PM Version 8 Directory list with the 6 programs Code:
|