get a list of index of selected items

get a list of index of selected items

luizhcastanho
Contributor Contributor
832 Views
10 Replies
Message 1 of 11

get a list of index of selected items

luizhcastanho
Contributor
Contributor

How can I generate a list of index of selected items? 

 

luizhcastanho_0-1718594519228.png

 

I found this function, but it returns a list with values:

 

;-------------------------------------------------------------------------------
; set_multilist_value - Sets SentVar$ to list of the items selected in SentList$
; Arguments: 2
;   SentList$ = String of the list variable name
;   SentVar$ = String of the variable name
; Syntax: (set_multilist_value "ListName" "Variable")
;-------------------------------------------------------------------------------
(defun set_multilist_value (SentList$ SentVar$ / SubList@)
  (setq SubList@ (eval (read SentList$)))
  (set (read SentVar$) (list (nth (atoi $value) SubList@)))
  (setq $value (substr $value (+ (strlen (itoa (atoi $value))) 2)))
  (while (/= $value "")
    (set (read SentVar$) (append (eval (read SentVar$))
      (list (nth (atoi $value) SubList@)))	 
    );set
    (setq $value (substr $value (+ (strlen (itoa (atoi $value))) 2)))
  );while
);defun set_multilist_value
0 Likes
833 Views
10 Replies
Replies (10)
Message 2 of 11

komondormrex
Mentor
Mentor
;-------------------------------------------------------------------------------
; get_list_indices - gets a list of indices from returned $value of list_box
; Arguments: 1
; SentString$ = String of the selected indices
; Syntax: (get_list_indices SentString$)
;-------------------------------------------------------------------------------
(defun get_list_indices (SentString$)
  (read (strcat "(" SentString$ ")"))
) 
0 Likes
Message 3 of 11

luizhcastanho
Contributor
Contributor

Sorry, I'm a beginner, how do I deploy the code? The variable that receives the list of selected items is Value1$ 

 

 (action_tile "List1" "(set_multilist_value \"List1@\" \"Value1$\")")

 

 

Using the image above as an example, (princ Value1$) return: ("LT14 (8+6)" "LT15 (10+5)" "LT16 (10+6)" "LT17 (10+7)")

but I would need you to return: ("3" "4" "5" "6")

0 Likes
Message 4 of 11

paullimapa
Mentor
Mentor

Look through all the examples given on this thread which uses comma or underline and in your case is a space as the delineator to covert a string to a list 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 11

ec-cad
Collaborator
Collaborator

Try this approach.

(setq lst (list
"LT11 (6=5)"
"LT12 (6+6)"
"LT14 (7+7)"
"LT14 (8+6)"
"LT15 (10+5)"
"LT16 (10+6)"
"LT17 (10+7)"
))

(setq slist (list "LT14 (8+6)" "LT15 (10+5)" "LT16 (10+6)" "LT17 (10+7)"))

(setq Index 0 ilist (list))

 (repeat (length lst)
  (setq check (nth Index lst))
  (if (member check slist)
   (setq ilist (cons (itoa Index) ilist)); add index to ilist
  ); if
  (setq Index (+ Index 1))
 ); repeat
 (if ilist
  (setq ilist (reverse ilist))
 ); if

 (princ "\n")
 (princ "\nOriginal List = ")
 (princ lst)
 (princ "\n")
 (princ "\nSelected List = ")
 (princ slist)
 (princ "\n")
 (princ "\nIndex List = ")
 (princ ilist)
 (princ)

ECCAD

0 Likes
Message 6 of 11

ec-cad
Collaborator
Collaborator

You can make a little function to find the Index Numbers (strings) from those lists.

Here's a sample of the function and how to use it..

 

;; get_index_list.lsp

;; Function: get_index_list
;; Usage: pass in List of Items - same as List sent to DCL ListBox
;;        pass in Selected List - those that operator choses in multi-select
;; Returns: ilist - list of Indexes of Selected List

 (defun get_index_list ( olist slist ); Original List, Selected List
  (setq Index 0 ilist (list))
  (repeat (length olist)
   (setq check (nth Index olist)); check = individual value of olist
   (if (member check slist); IS one of the Selected items
    (setq ilist (cons (itoa Index) ilist)); add index to ilist, e.g. 3 4 5 ...
   ); if
   (setq Index (+ Index 1)); set index to next value
  ); repeat
  (if ilist
   (setq ilist (reverse ilist))
  ); if
 ilist ; return the list, or nil
); function


;; Notes:
;; Once you have your (2) lists, SentVar$ and SentList$
;; You should be able to 'send' them to function get_index_list
;; As in:
 (action_tile "List1" "(set_multilist_value \"List1@\" \"Value1$\")")
 (setq Index_List (get_index_list SentList$ SentVar$))

 

ECCAD 

0 Likes
Message 7 of 11

john.uhden
Mentor
Mentor

How about just...

(mapcar '(lambda (x)(vl-position x lst)) slist)

John F. Uhden

Message 8 of 11

ec-cad
Collaborator
Collaborator

That's a LOT more compact than I can stand. 🙂

 

ECCAD

0 Likes
Message 9 of 11

john.uhden
Mentor
Mentor

@ec-cad ,

Thank you, I think. 😁

John F. Uhden

0 Likes
Message 10 of 11

ec-cad
Collaborator
Collaborator

Thanks,

Just 'pushing' the rope.

🙂

ECCAD

Message 11 of 11

john.uhden
Mentor
Mentor

Thanks, @ec-cad ,

Don't think I've ever heard that one before.  It will stick in the front of my mind. 😄

John F. Uhden

0 Likes