sum items with the same cdr from a list

sum items with the same cdr from a list

adaptacad
Advocate Advocate
1,652 Views
26 Replies
Message 1 of 27

sum items with the same cdr from a list

adaptacad
Advocate
Advocate

Hello again everyone.
I try a list
ex:

(setq lst
  '(
    (52.7553 . "ITEM_1")
    (110.412 . "ITEM_2") 
    (45.4596 . "ITEM_3") 
    (54.023 . "ITEM_4")
    (65.2474 . "ITEM_1") 
    (45.4596 . "ITEM_1") 
    (45.4596 . "ITEM_2") 
    (52.7553 . "ITEM_1")
    (110.412 . "ITEM_2") 
    (45.4596 . "ITEM_3") 
    (54.023 . "ITEM_4") 
    (65.2474 . "ITEM_1")
    (45.4596 . "ITEM_1") 
    (45.4596 . "ITEM_2")
  )
)

What is the best way to add the car of all items by associating the equivalent cdr:
((326,925 . "ITEM_1") ....

the same for all items

0 Likes
1,653 Views
26 Replies
Replies (26)
Message 21 of 27

john.uhden
Mentor
Mentor

@luizhcastanho ,

Your inslaje.lsp doesn't have lines beyond 400-something (at least in NotePad).

AND, I did not use (nth 1 x).

I'm so sorry, but I guess I misunderstood your objective.

John F. Uhden

0 Likes
Message 22 of 27

luizhcastanho
Contributor
Contributor

I sent the wrong file

0 Likes
Message 23 of 27

john.uhden
Mentor
Mentor

@luizhcastanho ,

One major thing I see wrong is that my combine function returns your desired list, but you are not capturing the return because new is local to the function.

You have only (combine lst)

whereas you should...

(setq <combined> (combine lst)) ;; <or some other symbol name>

John F. Uhden

0 Likes
Message 24 of 27

luizhcastanho
Contributor
Contributor

Same error 😓

error: bad argument type: numberp: "5"

 

You understood my problem correctly, your function works.

I'm just not able to implement it in my Lisp because of me 😂.

It's strange that it returns an error that was expecting a numeric variable showing "5"

 

0 Likes
Message 25 of 27

luizhcastanho
Contributor
Contributor

in my lisp file the list

 

lst (list '("140" "9" "6.3" "N1") '("70" "6" "6.3" "N1"))

 

I'm trying to use atoi function 

 

      (setq item (subst (apply '+ (mapcar '(lambda (x) (atoi (nth 1 x))) (cons item matches))) (atoi (nth 1 item)) item))

 

It doesn't show an error, but it doesn't add 🤣

0 Likes
Message 26 of 27

luizhcastanho
Contributor
Contributor

Worked, thanks

 

(defun combine (lst / item matches new)
  (while lst
    (setq item (car lst))
    (and
      (setq matches (vl-remove-if-not '(lambda (x)(= (nth 3 x)(nth 3 item))) (cdr lst)))
      (setq item (subst (itoa (apply '+ (mapcar '(lambda (x) (atoi (nth 1 x))) (cons item matches)))) (nth 1 item) item))
      (foreach match matches (setq lst (vl-remove match lst)))
    )
    (setq new (cons item new) lst (cdr lst))
  )
  (reverse new)
)

 

0 Likes
Message 27 of 27

john.uhden
Mentor
Mentor

@luizhcastanho ,

1st - I didn't recognize that this started back in July.  At that time, each item was just a dotted pair.

2nd - I had better check my code as to what is going wrong.

 

Oh, I think I see what it is.  This time your lst consists of items that contain 4 strings.

If you make up your mind I can fix my code (if necessary).

John F. Uhden

0 Likes