Jump to content

merge two Sublist of a master list and replace them by the merged one


handasa

Recommended Posts

Greetings everyone ...

 

i have a list consist of sublists

(

(B1 B2)

(B3 B4)

(B5 B6)

)

 

i need a way to merge two sublists of them say: (B1 B2)(B3 B4) together to be

(B1 B2 B3 B4)

 

AND THE FINAL MASTER LIST WILL LOOKS LIKE

(

(B1 B2 B3 B4)

(B5 B6)

)

 

the functions should supplied by the "nth" position of the two sublists which i need to merge...

 

thanks and best regards

Link to comment
Share on other sites

Assuming that the result should appear at the highest index:

(defun mergesublists ( idx lst / i l m )
   (setq m (apply 'max idx)
         i -1
   )
   (vl-remove nil
       (mapcar
          '(lambda ( x )
               (if (member (setq i (1+ i)) idx)
                   (progn (setq l (append l x)) (if (= m i) l))
                   x
               )
           )
           lst
       )
   )
)

 

_$ (setq lst '((B1 B2)(B3 B4)(B5 B6)))
((B1 B2) (B3 B4) (B5 B6))
_$ (mergesublists '(0 1) lst)
((B1 B2 B3 B4) (B5 B6))
_$ (mergesublists '(0 2) lst)
((B3 B4) (B1 B2 B5 B6))
_$ (mergesublists '(1 2) lst)
((B1 B2) (B3 B4 B5 B6))

Link to comment
Share on other sites

works fine .... thanks Mr. lee

i need a further more advice to fulfill my project please ... i can't remove an entire sublist such (B1 B2) from a master list contains other sublists

'( (B1 B2) (B3 B4) (B5 B6) )

using (vl-remove (nth 0 lst) lst) doesn't work for me ... any suggest ? and thanks in advance

Link to comment
Share on other sites

Handasa, the way you wrote it is correct. this will return the list without the element (and every occurances of the element too, be carefull). Look in my example how it removes both 0.

Command: (setq lst '(0 3 4 2 0 6 8))

(0 3 4 2 0 6 8)

Command: (vl-remove (nth 0 lst) lst)

(3 4 2 6 8)

One other thing: it returns the list without the element(s), but you didn't redefine (setq) lst with the result, which may be why you say it doesn't work.

(setq lst '(0 3 4 2 0 6 8))

(0 3 4 2 0 6 8)

Command: (vl-remove (nth 0 lst) lst)

(3 4 2 6 8)

Command: !lst

(0 3 4 2 0 6 8)

Command: (setq lst (vl-remove (nth 0 lst) lst))

(3 4 2 6 8)

Command: !lst

(3 4 2 6 8)

Link to comment
Share on other sites

Handasa, the way you wrote it is correct. this will return the list without the element (and every occurances of the element too, be carefull). Look in my example how it removes both 0.

 

One other thing: it returns the list without the element(s), but you didn't redefine (setq) lst with the result, which may be why you say it doesn't work.

 

Greetings , jef

... i checked it and it worked for me this time ... may be i was writing the code improperly ... thanks jef

Link to comment
Share on other sites

Assuming that the result should appear at the highest index :

 

Lee, why not use a boolean?

 

BTW considering the amount of such subfunctions you are posting - you are damn impressive list manipulator!

Link to comment
Share on other sites

Lee, why not use a boolean?

 

BTW considering the amount of such subfunctions you are posting - you are damn impressive list manipulator!

 

Thank you Grrr - but where are you suggesting the use of the boolean? In what way?

Link to comment
Share on other sites

Thank you Grrr - but where are you suggesting the use of the boolean? In what way?

 

So the result could appear at the lowest/highest index, depending on it - i.e.:

 

_$ (setq lst '((B1 B2)(B3 B4)(B5 B6)))
((B1 B2) (B3 B4) (B5 B6))

_$ (mergesublists '(0 2) lst nil)
((B3 B4) (B1 B2 B5 B6))

_$ (mergesublists '(0 2) lst T)
((B1 B2 B5 B6) (B3 B4))

 

But after rethinking, the user might supply list for the "idx" argument like: '(0 5 8 9)

So my suggestion might be not good.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...