Author Topic: How to convert oldlst into newlst?  (Read 6363 times)

0 Members and 1 Guest are viewing this topic.

xianaihua

  • Guest
How to convert oldlst into newlst?
« on: November 13, 2011, 08:40:54 AM »
Hi,all!
How to convert oldlst into newlst? that is will exchange rows and columns.
Thank you!
Code: [Select]
(setq oldlst'((("num" . "1") ("code" . "CXJ.1.1-01") ("name" . "part1") ("numb" . "1") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "2") ("code" . "CXJ.1.1-02") ("name" . "part2") ("numb" . "2") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "3") ("code" . "CXJ.1.1-03") ("name" . "part3") ("numb" . "3") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "4") ("code" . "CXJ.1.1-04") ("name" . "part4") ("numb" . "4") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "5") ("code" . "CXJ.1.1-05") ("name" . "part5") ("numb" . "5") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "6") ("code" . "CXJ.1.1-06") ("name" . "part6") ("numb" . "6") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "7") ("code" . "CXJ.1.1-07") ("name" . "part7") ("numb" . "7") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "A1") ("code" . "CXJ.1.1.1-00") ("name" . "asm1") ("numb" . "1") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "A2") ("code" . "CXJ.1.1.2-00") ("name" . "asm2") ("numb" . "2") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "A3") ("code" . "CXJ.1.1.3-00") ("name" . "asm3") ("numb" . "3") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "A4") ("code" . "CXJ.1.1.4-00") ("name" . "asm4") ("numb" . "4") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "B1") ("code" . "GB/T5780-2000") ("name" . "bolt M10X45") ("numb" . "5") ("mate" . "steel") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "B2") ("code" . "GB/T6170-2000") ("name" . "nut M10") ("numb" . "5") ("mate" . "steel") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "B3") ("code" . "GB/T93-1987") ("name" . "washer 10") ("numb" . "5") ("mate" . "65Mn") ("wein" . "") ("weia" . "") ("note" . ""))
(("num" . "B4") ("code" . "GB/T95-2002") ("name" . "washer 10") ("numb" . "5") ("mate" . "steel") ("wein" . "") ("weia" . "") ("note" . ""))
))
(setq newlst'(("num" "1" "2" "3" "4" "5" "6" "7" "A1" "A2" "A3" "A4" "B1" "B2" "B3" "B4")
("code" "CXJ.1.1-01" "CXJ.1.1-02" "CXJ.1.1-03" "CXJ.1.1-04" "CXJ.1.1-05" "CXJ.1.1-06" "CXJ.1.1-07" "CXJ.1.1.1-00" "CXJ.1.1.2-00" "CXJ.1.1.3-00" "CXJ.1.1.4-00" "GB/T5780-2000" "GB/T6170-2000" "GB/T93-1987" "GB/T95-2002")
("name" "part1" "part2" "part3" "part4" "part5" "part6" "part7" "asm1" "asm2" "asm3" "asm4" "bolt M10X45" "nut M10" "washer 10" "washer 10")
("numb" "1" "2" "3" "4" "5" "6" "7" "1" "2" "3" "4" "5" "5" "5" "5")
("mate" "Q235-A" "40Cr" "60Si2Mn" "1Cr18Ni9Ti" "HT250" "ZG1Cr18Ni9Ti" "40Cr" "" "" "" "" "steel" "steel" "65Mn" "steel")
("wein" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
("weia" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
("note" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #1 on: November 13, 2011, 08:50:24 AM »
Code: [Select]
(mapcar (function (lambda (a b) (cons a (mapcar (function cdr) b))))
        (mapcar (function car) (car l))
        (apply (function mapcar) (cons (function list) l))
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #2 on: November 13, 2011, 08:54:28 AM »
 :-)
Code: [Select]
(setq l oldlst)

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #3 on: November 13, 2011, 09:24:13 AM »
Great! !!
Thank ElpanovEvgeniy  for your help

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #4 on: November 13, 2011, 10:34:45 AM »
recursion variant:
Code: [Select]
(defun f (l)
  (if (car l)
    (cons (cons (caaar l) (mapcar (function cdar) l)) (f (mapcar (function cdr) l)))
  )
)

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #5 on: November 13, 2011, 10:56:59 AM »
Hi,ElpanovEvgeniy!
In addition,I need your help
How to  moving, delete, insert the operation for oldlst, but Don't change item "num" ?

Again for help!
Think you !

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #6 on: November 13, 2011, 11:38:17 AM »
Hi,ElpanovEvgeniy!
In addition,I need your help
How to  moving, delete, insert the operation for oldlst, but Don't change item "num" ?

Again for help!
Think you !

I'm not sure I understand your job...
my English is not as good as Lisp  :-(

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #7 on: November 13, 2011, 11:47:50 AM »
That is to say, in the oldlst insert (move, delete) after a item(row), one of the column "num" order unchanged.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #8 on: November 13, 2011, 11:53:01 AM »
That is to say, in the oldlst insert (move, delete) after a item(row), one of the column "num" order unchanged.

show an example

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #9 on: November 13, 2011, 11:58:34 AM »
See below
« Last Edit: November 13, 2011, 12:10:47 PM by xianaihua »

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: How to convert oldlst into newlst?
« Reply #10 on: November 13, 2011, 12:35:52 PM »
Variant to just swap rows and columns - remain dotted pairs :

Code: [Select]
(defun ff (l)
  (if (car l)
    (cons (mapcar (function car) l) (ff (mapcar (function cdr) l)))
  )
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: How to convert oldlst into newlst?
« Reply #11 on: November 13, 2011, 01:18:11 PM »
Try this :

Code: [Select]
(defun fff (oldlst newrow)
  (mapcar '(lambda ( x ) (if (equal (car newrow) (car x)) (setq oldrow x))) oldlst)
  (subst newrow oldrow oldlst)
)

Code: [Select]
(setq newrow '(("num" . "7") ("insert" . "") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))

Code: [Select]
(fff oldlst newrow)

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #12 on: November 13, 2011, 11:19:15 PM »
Hi,ribarm!
Thank your help.
your function 'ff' is very nice,but function 'fff' is wrong.
your function 'ff' and  'fff' is wrong
this is my function :
Code: [Select]
(defun StrParse_lst (str / pre_str num_str)
  (cond ((numberp (read str))
(setq pre_str ""
       num_str str
) ;_ 结束setq
)
((wcmatch str "A*,B*")
(setq pre_str (substr str 1 1)
       num_str (substr str 2)
) ;_ 结束setq
)
  ) ;_ 结束cond

  (list pre_str num_str)
)


(defun Add_Lst_Item (AddItemLst lst / pre_str num_str valstr oldpre_str oldnum_str sublst ret_lst)
  (setq pre_str (nth 0 (StrParse_lst (cdar AddItemLst)))
num_str (nth 1 (StrParse_lst (cdar AddItemLst)))
  ) ;_ 结束setq
  (mapcar
    '(lambda (item)
       (setq oldpre_str (nth 0 (StrParse_lst (cdar item)))
     oldnum_str (nth 1 (StrParse_lst (cdar item)))
       ) ;_ 结束setq
       (cond
((and (= pre_str oldpre_str)
       (>= (atoi oldnum_str) (atoi num_str))
  ) ;_ 结束and
  (setq
    valstr (strcat pre_str
   (itoa (1+ (atoi oldnum_str)))
   ) ;_ 结束strcat
    sublst (subst (vl-list* (caar item) valstr)
  (car item)
  item
   ) ;_ 结束subst
  ) ;_ 结束setq
)
(t (setq sublst item))
       ) ;_ 结束cond

       (if (and (= pre_str oldpre_str)
(= (atoi num_str) (atoi oldnum_str))
   ) ;_ 结束and
(setq ret_lst (cons sublst (cons AddItemLst ret_lst)))
(setq ret_lst (cons sublst ret_lst)
) ;_ 结束setq
       ) ;_ 结束if
     ) ;_ 结束lambda
    lst
  ) ;_ 结束mapcar
  (reverse ret_lst)
) ;_ 结束defun
test
Code: [Select]
(setq newrow1 '(("num" . "7") ("code" . "insert1") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))
(setq newrow2 '(("num" . "A1") ("code" . "insert2") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))
(setq newrow3 '(("num" . "B2") ("code" . "insert3") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))

(setq lst1 (Add_Lst_Item newrow1 oldlst)
      lst2 (Add_Lst_Item newrow2 oldlst)
      lst3 (Add_Lst_Item newrow3 oldlst)
) ;_ 结束setq
« Last Edit: November 15, 2011, 09:49:44 PM by xianaihua »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to convert oldlst into newlst?
« Reply #13 on: November 14, 2011, 02:53:03 AM »
xianaihua, similar to your code:

Code: [Select]
(defun f (n l)
  (cond ((not l) nil)
        ((equal (caar l) (car n)) (f nil (cons n l)))
        ((equal (caar l) (caadr l))
         (cons (car l)
               (f n
                  (cons (cons (cons (caaar l)
                                    (if (wcmatch (cdaar l) "#*")
                                      (itoa (1+ (atoi (cdaadr l))))
                                      (strcat (substr (cdaadr l) 1 1) (itoa (1+ (atoi (substr (cdaadr l) 2)))))
                                    )
                              )
                              (cdadr l)
                        )
                        (cddr l)
                  )
               )
         )
        )
        ((cons (car l) (f n (cdr l))))
  )
)

but you did not explain what to do in such cases?
Code: [Select]
(setq newrow1 '(("num" . "9") ("code" . "insert2") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))

xianaihua

  • Guest
Re: How to convert oldlst into newlst?
« Reply #14 on: November 14, 2011, 10:37:35 PM »
ElpanovEvgeniy
Your program is great, the program is just what I want. Thank you very much, let me learn a lot
 :lmao: