TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: xianaihua 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!
(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" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
))
-
(mapcar (function (lambda (a b) (cons a (mapcar (function cdr) b))))
(mapcar (function car) (car l))
(apply (function mapcar) (cons (function list) l))
)
-
:-)
(setq l oldlst)
-
Great! !!
Thank ElpanovEvgeniy for your help
-
recursion variant:
(defun f (l)
(if (car l)
(cons (cons (caaar l) (mapcar (function cdar) l)) (f (mapcar (function cdr) l)))
)
)
-
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 !
-
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 :-(
-
That is to say, in the oldlst insert (move, delete) after a item(row), one of the column "num" order unchanged.
-
That is to say, in the oldlst insert (move, delete) after a item(row), one of the column "num" order unchanged.
show an example
-
See below
-
Variant to just swap rows and columns - remain dotted pairs :
(defun ff (l)
(if (car l)
(cons (mapcar (function car) l) (ff (mapcar (function cdr) l)))
)
)
-
Try this :
(defun fff (oldlst newrow)
(mapcar '(lambda ( x ) (if (equal (car newrow) (car x)) (setq oldrow x))) oldlst)
(subst newrow oldrow oldlst)
)
(setq newrow '(("num" . "7") ("insert" . "") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))
(fff oldlst newrow)
M.R.
-
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 :
(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
(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
-
xianaihua, similar to your code:
(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?
(setq newrow1 '(("num" . "9") ("code" . "insert2") ("name" . "") ("numb" . "") ("mate" . "") ("wein" . "") ("weia" . "") ("note" . "")))
-
ElpanovEvgeniy
Your program is great, the program is just what I want. Thank you very much, let me learn a lot
:lmao:
-
this is my job.
-
ElpanovEvgeniy
Good weekend!
If you have time, please optimization or simplify my program In the following posts。
Thanks agan!
http://www.theswamp.org/index.php?topic=31524.msg452218#msg452218 (http://www.theswamp.org/index.php?topic=31524.msg452218#msg452218)