Ignore blank lines in mapconcat?

I am trying to insert a comma separated string created from multiple values ​​entered by the user. If a certain value is empty then I don't want to insert that value. My problem is that it mapconcat

still inserts the delimiter when the function returns nil

. I also tried to do this with a control unless

in the list instead of a function with the same result.

(insert (mapconcat 
         (function (lambda (x) (unless (string-equal x "") x))))
         (list input-a input-b input-c)
         ", "))

      

If the values ​​are from the user "foo"

, ""

and "bar"

, the output will be "foo, , bar"

; I wish it were "foo, bar"

. How can I prevent the delimiter from being included when the input value is empty?

+3


source to share


1 answer


Your list contains values nil

, but mapconcat

it will still process them, so you first need to remove unnecessary items from the list.

Exactly how you do this will depend on whether you want to keep the list in its original form. Here's a parameter that doesn't change the original list:



(setq my-list (list "foo" "" "bar"))
(mapconcat 'identity
           (remove "" my-list)
           ", ")

      

If you don't need the original, you can use delete

instead remove

; but make sure you read the help for delete

if you do.

+6


source







All Articles