Post Reply 
(50G) Ver8. Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE
10-21-2024, 07:14 PM (This post was last modified: 11-10-2024 03:33 PM by Gil.)
Post: #1
(50G) Ver8. Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE
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).
Find all posts by this user
Quote this message in a reply
10-23-2024, 01:36 PM (This post was last modified: 11-10-2024 02:28 PM by Gil.)
Post: #2
RE: 50G Lists Manipulation

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
Find all posts by this user
Quote this message in a reply
11-10-2024, 01:57 PM (This post was last modified: 11-10-2024 08:13 PM by Gil.)
Post: #3
RE: (50G) Lists Manipulation REPLACE, SWAP, KEEP, ADD, DELETE
Version 8

Directory list with the 6 programs
Code:

DIR
  L.SWAP
  \<< "3/2 Arg: List & a/b
a) \|>Pos.i \|>Pos.k
b) or \|>{Pos.i Pos.k}
       (as a list)
" DROP 0 \-> p1 p2 L
    \<< p2 TYPE 5 == p2 SIZE 2 == AND
      IF
      THEN p1 p2 OBJ\-> DROP
      ELSE p1 p2
      END 3 \->LIST { L p1 p2 } STO L p1 GET L p2 GET L p1 ROT PUT p2 ROT PUT
    \>>
  \>>
  L.DEL
  \<< "3/2 Arg: List & a/b/c
a)\|>Pos.beg \|>Pos.end
(from Posbeg \-> Posend)
b)or \|>{Pos.i Pos.k }
      (as a list)
c)or just Pos or {Pos}
" DROP
    IFERR \<-keep
    THEN 0
    END \-> L l \<-keep
    \<<
      CASE L TYPE 5 == l TYPE 5 \=/ AND
        THEN l 1 \->LIST 'l' STO
        END L TYPE 5 \=/ l TYPE 5 \=/ AND
        THEN { } L l MIN L l MAX
          FOR i i +
          NEXT 'l' STO 'L' STO
        END
      END l 1. * 'l' STO { } 1 L SIZE
      FOR i l i POS 0 \<-keep 1 ==
        IF
        THEN \=/
        ELSE ==
        END
        IF
        THEN L i GET DUP TYPE 5 == { 1 \->LIST } IFT +
        END
      NEXT
    \>>
  \>>
  L.KEEP
  \<< "3/2 Arg: List & a/b/c
a)\|>Pos.beg \|>Pos.end
(from Posbeg \-> Posend)
b)or \|>{Pos.i Pos.k }
      (as a list)
c)or just Pos or {Pos}
" DROP 1 \-> \<-keep
    \<< L.DEL
    \>>
  \>>
  L.ADD
  \<< "3 Arg: 
\|>List
\|>PosStart (not after)
\|>{El.1 El.2 }
 or just Elem
" DROP 0 \-> L p l S
    \<<
      IF l TYPE 5 \=/ l TYPE 5 == l SIZE 1 \<= AND OR
      THEN l 1 \->LIST 'l' STO
      END L SIZE 'S' STO
      CASE p S >
        THEN L l +
        END p 1 ==
        THEN l L +
        END { } 1 p 1 -
        FOR i L i GET DUP TYPE 5 == { 1 \->LIST } IFT +
        NEXT { } p S
        FOR i L i GET DUP TYPE 5 == { 1 \->LIST } IFT +
        NEXT l SWAP + +
      END
    \>>
  \>>
  L.RPL
  \<< "4/3 Arg: List & 1+2

  1) What to be repl:
  a) \|>Pos.beg \|>Pos.end
(from Posbeg \-> Posend)
or b) \|>{Pos.i Pos.k }
       (as a list)
or c)  Pos or {Pos}

  And 2) New:
  \|>{El.I El.K }
or just Elem or {Elem}

To delete El. in Pos.j
1) {Pos.i Pos.j Pos.k}
2) {I empty.string K}
   \-> Result: { I K }

To delete a pos, d var
below can be modified;
default: empty.string

To use, you must have:
L.DEL & L.ADD !
" DROP "" \-> d
    \<< OVER DUP TYPE 5 == SWAP SIZE 1 == AND
      IF
      THEN SWAP 1 GET SWAP
      END DUP2 TYPE SWAP TYPE * 25 ==
      IF
      THEN DUP SIZE \-> l1 l2 s
        \<< l1 HEAD 1 \->LIST 2 s
          FOR i l1 l2 i 1 - GET DUP TYPE 5 ==
            IF
            THEN SIZE 1 - ADD DUP 'l1' STO
            ELSE DROP
            END i GET +
          NEXT 'l1' STO 1 s
          FOR i l1 i GET DUP UNROT L.DEL SWAP l2 i GET DUP dd SAME NOT
            IF
            THEN L.ADD
            ELSE DROP2 l1 -1 ADD 'l1' STO
            END
          NEXT
        \>>
      ELSE PICK3 DUP TYPE 5 ==
        IF
        THEN DROP OVER 4 ROLLD 4 ROLLD
        ELSE PICK3 MIN 5 ROLLD 5 ROLLD
        END L.DEL SWAP ROT DUP d SAME NOT
        IF
        THEN L.ADD
        ELSE DROP2
        END
      END
    \>>
  \>>
  LeRPL
  \<< "3 Arg: List & 1+2
 
 List & occurrences 
 of specific elem a b
    olda \-> newA
    oldb \-> newB 

 Repl all repeated occ
 1) {a b} 2) {A B}
 or 1) a 2) A

 Repl # limited occurr
 1){\oo# a b} 2) {A B}
 or 1) {\oo# a} 2) A
 #:#MaxReplac for a/b
 # preceded by \oo sign

 If b\->B c\->deleted d\->D
 {b c d ) 1){b c d}
 2) {B empty.string D}
   \-> Result: { B D }

To delete Elem, var d
below can be modified;
default: empty.string

To use, you must have
L.DEL & L.ADD !

" DROP 0 0 0 0 0 "" \-> L l1 l2 s1 c cf p ct d
    \<<
      IF l1 TYPE 5 \=/ l1 TYPE 5 == l1 SIZE 1 == AND OR
      THEN l1 1 \->LIST 'l1' STO 1
      ELSE l1 SIZE
      END 's1' STO
      IF l2 TYPE 5 \=/ l2 TYPE 5 == l2 SIZE 1 \<= AND OR
      THEN l2 1 \->LIST 'l2' STO
      END
      IF l1 HEAD DUP TYPE 6 ==
      THEN \->STR 'cf' STO
        IF cf 2 2 SUB "\oo" SAME
        THEN l1 1 L.DEL 'l1' STO -1 's1' STO+ cf 3 cf SIZE 1 - SUB OBJ\-> L SIZE MIN
        ELSE L SIZE
        END
      ELSE DROP L SIZE
      END 'cf' STO L 1 s1
      FOR i 0
        WHILE cf <
        REPEAT DUP l1 i GET POS DUP 'p' STO 0 \=/
          IF
          THEN 1 'c' STO+ 1 'ct' STO+ p L.DEL p l2 i GET DUP "" SAME NOT
            IF
            THEN L.ADD
            ELSE DROP2
            END c
          ELSE cf
          END
        END 0 'c' STO
      NEXT ct "Changes" \->TAG
    \>>
  \>>
END


Attached File(s)
.hp  LIST8.hp (Size: 4.03 KB / Downloads: 2)
Find all posts by this user
Quote this message in a reply
Post Reply 




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