Search for a item in several lists

Search for a item in several lists

Anonymous
Not applicable
2,733 Views
17 Replies
Message 1 of 18

Search for a item in several lists

Anonymous
Not applicable

 Hello 

 

I would like to create a routine that I have a inicial value that should be searched in several diferent lists and if found, returns the third element of this list. The next search will start with this third element and so on. I'm added a spreadshhet as a example what i need. Somebody that has a good knowledge of list can support me?

 

EXAMPLE.jpg

0 Likes
2,734 Views
17 Replies
Replies (17)
Message 2 of 18

cadffm
Consultant
Consultant
There are many good people here who can help, but I can not find your current program code?

Sebastian

0 Likes
Message 3 of 18

Anonymous
Not applicable
Thanks for your feedback, but It is exactly my problem. I could generate the Matrix in Excel. So now the Idea is Export It to a .CSV file and import in autolisp list. Then create a routine to put the lists in sequency and export to a TXT or Excel file. Are you able How to do this routine?
0 Likes
Message 4 of 18

cadffm
Consultant
Consultant

Then I misunderstood your question, I thought you needed help with some programming issues.

And you do not need to export anything but CSV or text (unless you want that),
you can read Lisp via ActiveX directly Excel files write manipulate save etc. if

excel is installed.

 

" Are you able How to do this routine? "

Yes, that's not a problem, but I wanted to help only with difficulties (this is actually a forum about programming - in my opinion),
and I'm not interested in writing a program for someone "for fun", sorry.

But there are plenty of others who do that again and again, I keep my fingers crossed for you!

Sebastian

Message 5 of 18

cadffm
Consultant
Consultant

One question:

What does that have to do with AutoCAD, nothing?
You have data in Excel and you need it in another form again in Excel,
you do not need the data in the CAD, right?

Then maybe you should ask in an EXCEL-VBA forum!

Sebastian

0 Likes
Message 6 of 18

Anonymous
Not applicable
Sorry, I think that i couldn't explain exactly. Let me try again. I have a TXT file with a lot of information. So I choosen Excel to import this .TXT file and manipulate the strings till have this Matrix. So now i need to organize this Matrix sequentially. For this there are 2 ways:
1-) create some VBA code, but i'm not a VBA programer;
2) create a Lisp routine to read this matrix from Excel and make It sequentially using lists manipulation inside this routine. After this i can continue programing the rest of the routine with draw commands.
Is It possible? I only need this Matrix organized sequentially, the rest i know How to do.
0 Likes
Message 7 of 18

ronjonp
Mentor
Mentor

Post your text file .. sorting lists is generally pretty easy.

0 Likes
Message 8 of 18

john.uhden
Mentor
Mentor
(defun search (item lists)
 (vl-remove-if-not '(lambda (x)(vl-position item x)) lists)
)
Command: (setq a '(1 2 3) b '(2 3 4) c '(3 4 5))
(3 4 5)

Command: (search 2 (list a b c))
((1 2 3) (2 3 4))

But if the values have any 'REALs then we have to add in a little (equal) action, which I haven't yet figured out.

John F. Uhden

0 Likes
Message 9 of 18

john.uhden
Mentor
Mentor

This may just work for lists of any type:

(defun search (item lists fuzz)
  (vl-remove-if-not '(lambda (x)(vl-some '(lambda (z)(equal z item fuzz)) x)) lists)
)

Command: (setq a '(1.25 2.12 3.33) b '(0.44 1.98 5.25) c '("A" 3 4))
("A" 3 4)

Command: (search 2 (list a b c) 0.13)
((1.25 2.12 3.33) (0.44 1.98 5.25))

Command: (search "A" (list a b c) 0.13)
(("A" 3 4))

John F. Uhden

0 Likes
Message 10 of 18

Anonymous
Not applicable

Thank you John, the search is working.

Now I could generate a list from the matrix, then search the initial value in the list. Other doubt is if I have more than 1 result in the search, so I need to store the result in more than one variable to do the next search and so on. In the image that I attached in the first post, there are 2 result as "TK_1". How is the best way to do this cascate searching?

0 Likes
Message 11 of 18

john.uhden
Mentor
Mentor
I don't know what you mean by cascade searching.
You can store the result(s) any way you want.
My tendency is to leave multiple hits in one list.
If you need to save multiple lists, I would still use one variable, but
prefix each list with the item for which you were searching. For example,
say you were searching for "A" and the search returned '(("A" 1 2 3)( 4 "A"
5 6)) and we call it 'found'.
Then just (setq found (cons "A" found)) ... '("A" ("A" 1 2 3)(4 "A" 5 6))
And if you are doing multiple searches then with each find (setq founds
(cons found founds)). It's just a list of lists, and that's what AutoLisp
is all about.

John F. Uhden

0 Likes
Message 12 of 18

cadffm
Consultant
Consultant

@Anonymous  schrieb:

How is the best way to do this cascate searching?


Hi brucarboni,

 

your sample on the right is only a part of the result and i am not sure how looks the full result (and in which order!)

and there is not "the one only way" , so i can only link to the functions mapcar, foreach, while, repeat.. 

 

For example, all 43 rows from the left you will find in the following lists,

but is this the order you want? The first three are clear.

___

 

("X646X500_X" "48,-282" "TK_1" "129,-297" "46.06" "32165K1MU" "BAR CODE WT-PK")
("TK_1" "129,-297" "GR_1" "234,-282" "40.61" "BAR CODE WT-PK" "320455ATV")
("GR_1" "234,-282" "GR_2" "279,-291" "68" "320455ATV" "")
("GR_2" "279,-291" "RCP_D" "345,-291" "90" "" "")
("RCP_D" "345,-291" "C_1" "381,-345" "20" "" "32035207V")
("C_1" "381,-345" "TK_2" "435,-291" "47.13" "32035207V" "")
("TK_2" "435,-291" "M617LHD_LF" "435,-135" "135.82" "" "3216524W3")
("M617LHD_LF" "" "" "" "" "" "")
-
("TK_1" "129,-297" "SP6134_B_X" "" "54.4" "BAR CODE WT-PK" "X1::UWELD")
("SP6134_B_X" "" "X646X502_X" "132,-408" "52.61" "X1::UWELD" "321683TV1")
("X646X502_X" "" "" "" "" "" "")
-
("TK_2" "435,-291" "SP3409_X1" "" "76" "" "X1::UWELD")
("SP3409_X1" "" "SP3410_X1" "" "51.9" "X1::UWELD" "X1::UWELD")
("SP3410_X1" "" "C_2" "393,-477" "57.36" "X1::UWELD" "320350A50")
("C_2" "393,-477" "X644X312_X" "366,-612" "108.93" "320350A50" "32165K28Z")
("X644X312_X" "" "" "" "" "" "")

 

and THEN?
please check my attachment for all listings and say something about this and your wanted order.

 

 

Sebastian

0 Likes
Message 13 of 18

Anonymous
Not applicable

Hi

 

Thank you for your response. It's is perfect according your attachment, the order. The routine should read the initial value and then make the logic according your .pdf attachement. Do you think that is possible? I'm thinking how to do this for this 43 rows, but sometimes i can have more or less rows.

 

 

0 Likes
Message 14 of 18

cadffm
Consultant
Consultant


Oh, what a coincidence - it is not made by hand.

 

(defun createnewlist (initial)  ..)
    create a new list and DELETE the items from source-list
)

if (createnewlist "initialstring" )

do for each item in that new list

   if (createnewlist "initialstring" )

      and do the same with each new list..
  


Unfortunately, I am currently somewhere else, but I like to get back to you.

 

 

 

Sebastian

0 Likes
Message 15 of 18

Anonymous
Not applicable

Thanks Sebastian. When you come back if you can support me on that it will be helpful.

0 Likes
Message 16 of 18

cadffm
Consultant
Consultant
Do you have your code ready, whether it's nice or not does not matter. Show us your current code and we'll see where it goes more elegant.

Sebastian

0 Likes
Message 17 of 18

Anonymous
Not applicable

I was starting something like below:

 

(defun GetCells()

;;;--- Function to retrieve values for a cell or a range of cells
;;;
;;; To retrieve a cells value:
;;; Usage (getCellFunction "C:\\MYFILE.xlsxm" "Sheet1" "A3")
;;;
;;; To retrieve values for a range of cells
;;; Usage (getCellFunction "C:\\MYFILE.xlsxm" "Sheet1" "A2:A4")
;;;
(vl-load-com)
(defun getCellsFunction(fileName sheetName cellName / myXL myBook mySheet myRange cellValue)
(setq myXL(vlax-get-or-create-object "Excel.Application"))
(vla-put-visible myXL :vlax-false)
(vlax-put-property myXL 'DisplayAlerts :vlax-false)
(setq myBook (vl-catch-all-apply 'vla-open (list (vlax-get-property myXL "WorkBooks") fileName)))
(setq mySheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property myBook "Sheets") "Item" sheetName)))
(vlax-invoke-method mySheet "Activate")
(setq myRange (vlax-get-property (vlax-get-property mySheet 'Cells) "Range" cellName))
(setq cellValue(vlax-variant-value (vlax-get-property myRange 'Value2)))
(vl-catch-all-apply 'vlax-invoke-method (list myBook "Close"))
(vl-catch-all-apply 'vlax-invoke-method (list myXL "Quit"))
(if (not (vlax-object-released-p myRange))(progn(vlax-release-object myRange)(setq myRange nil)))
(if (not (vlax-object-released-p mySheet))(progn(vlax-release-object mySheet)(setq mySheet nil)))
(if (not (vlax-object-released-p myBook))(progn(vlax-release-object myBook)(setq myBook nil)))
(if (not (vlax-object-released-p myXL))(progn(vlax-release-object myXL)(setq myXL nil)))
(if(= 'safearray (type cellValue))
(progn
(setq tempCellValue(vlax-safearray->list cellValue))
(setq cellValue(list))
(if(= (length tempCellValue) 1)
(progn
(foreach a tempCellValue
(if(= (type a) 'LIST)
(progn
(foreach b a
(if(= (type b) 'LIST)
(setq cellValue(append cellValue (list (vlax-variant-value (car b)))))
(setq cellValue(append cellValue (list (vlax-variant-value b))))
)
)
)
(setq cellValue(append cellValue (list (vlax-variant-value a))))
)
)
)
(progn
(foreach a tempCellValue
(setq tmpList(list))
(foreach b a
(setq tmp(vlax-variant-value b))
(setq tmpList(append tmpList (list tmp)))
)
(setq cellValue(append cellValue tmpList))
)
)
)
)
)
cellValue
)

;;;--- Get the excel file
;(setq fileName(getfiled "Select Excel File" "" "*" 16))

;;;--- Get the sheet name
;(setq sheetName(getstring T "\nName of sheet: "))

;;;--- Cycle while the user enters cell addresses
;(while(/= "" (setq cellName(getstring "\nCell address to retrieve? [ Examples: A2 or A1:B4 ]:")))

;;;--- Get the value of the cell or cells
;(setq cellValue(getCellsFunction fileName sheetName cellName))

;;;--- Display the value of the cell(s)
;(princ (strcat "\n The value of address " cellName " is: "))(princ cellValue)
😉
;(princ)
)

;-----------------------------------------------------------------------------------------------------------------

; searching function

(defun search (item lists fuzz)

(vl-remove-if-not '(lambda (x)(vl-some '(lambda (z)(equal z item fuzz)) x)) lists)
)

;-----------------------------------------------------------------------------------------------------------------
; routine to export values from excel sheet to a .txt file

 

(GetCells)

(setq matriztxt nil)
(setq matriztxt (open "C:\\overlay-100\\matriz-naoseq.txt" "W"))
(setq linhas_matriz (fix (getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" "C2")))
(setq celinimatriz 3);2

(repeat linhas_matriz
(write-line (strcat

(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "D" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "E" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "F" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "G" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "H" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "I" (itoa celinimatriz)))
" "
(getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "resume" (strcat "J" (itoa celinimatriz)))

)
matriztxt)

(setq celinimatriz (+ celinimatriz 1))
(setq linhas_matriz (- linhas_matriz 1))

);end of repeat function
(close matriztxt)

(setq listmatriz nil)
(setq matriz (open "C:\\overlay-100\\matriz-naoseq.txt" "r"))
(while
(setq nl_matriz (read-line matriz))
(setq listmatriz (cons nl_matriz listmatriz))
)
(close matriz)

(car (list (car listmatriz)))

 

(setq valorini (getCellsFunction "C:\\overlay-100\\overlay-100.xlsx" "! Harness name information" "E6"))

(search valorini (list listmatriz) 0.13)

 

 

; I have stopped here, but it's necessary to continue the searching.

0 Likes
Message 18 of 18

roland.r71
Collaborator
Collaborator

Next time you post some code, use the "insert code" button </> on the menu bar. Please.

It keeps the indentation intact, for one. (that's assuming you do structure your code)

(it
   (and reads looks
      (a lot)
      (better)
   )
   (like this)
)