Jump to content

Recommended Posts

Posted

Hi

Can someone please post a simple code showing how to access a entries in list with in a list?

 

Thanks

Posted

@Moderator: Please rename thread to 'N[highlight]e[/highlight]sted List' so that it can be found in future.

 

@samifox: Please provide an example of the list in question and the items to be retrieved.

Posted

Ok

 

2 things

 

the best way to retrive B1

 

and...let say i want to boolean test if C2 exist in any of the sublist

(setq lst (list (list 'A1 'A2 )(list 'B1 'B2) (list 'C1 'C2)))

Posted
best way to retrive B1

 

_$ (setq lst '((a1 a2) (b1 b2) (c1 c2)))
((A1 A2) (B1 B2) (C1 C2))
_$ (car (assoc 'b1 lst)) ;; Position unknown
B1
_$ (car (nth 1 lst)) ;; Position known
B1

 

let say i want to boolean test if C2 exist in any of the sublist

 

_$ (setq lst '((a1 a2) (b1 b2) (c1 c2)))
((A1 A2) (B1 B2) (C1 C2))
_$ (vl-some '(lambda ( x ) (member 'c2 x)) lst)
(C2)

Posted

in the help reference for the vl-some() there is an example i dont fully undertand

The following example checks whether nlst (a number list) has equal elements in sequence:

 

$ (setq nlst (list 0 2 pi pi 4))

(0 2 3.14159 3.14159 4)

_$ (vl-some '= nlst (cdr nlst))

T

for my sense it look as following,

vl-some retrive the first element in the ntls list (0) and compare it against the first element in (cdr ntls) (2) list , if the test is nil, the next element in (cdr(nlst)) (3) is being compared to the first element of ntls.

 

if all elements in (cdr(ntls)) list been evaluated , vl some get the next element of the ntls list

 

all of this loops until a non-nil value found

 

 

is that true ?

Posted

$ ([color=blue]setq[/color] nlst ([color=blue]list [/color]0 2 [color=blue]pi[/color] [color=blue]pi[/color] 4))
(0 2 3.14159 3.14159 4)
_$ ([color=blue]vl-some[/color] '[color=blue]=[/color] nlst ([color=blue]cdr[/color] nlst))
[color=blue]T[/color]

Here is the evaluation sequence:

([color=blue]vl-some[/color] '[color=blue]=[/color] nlst ([color=blue]cdr[/color] nlst)) --> ([color=blue]vl-some[/color] '[color=blue]=[/color] '(0 2 [color=blue]pi pi [/color]4) '(2 [color=blue]pi pi [/color]4))

([color=blue]=[/color] 0  2 ) => [color=blue]nil[/color]
([color=blue]=[/color] 2  [color=blue]pi[/color]) => [color=blue]nil[/color]
([color=blue]=[/color] [color=blue]pi[/color] [color=blue]pi[/color]) => [color=blue]T[/color] [color=green];; cease evaluation[/color]

If all elements were to be processed:

_$ ([color=blue]vl-some[/color] '([color=blue]lambda[/color] ( a b ) ([color=blue]print [/color]a) ([color=blue]princ [/color]b) [color=blue]nil[/color]) nlst ([color=blue]cdr [/color]nlst))

0 2
2 3.14159
3.14159 3.14159
3.14159 4
[color=blue]nil[/color]

Hence, for a general list:

([color=blue]vl-some[/color] '([color=blue]predicate-function[/color] ( arg-1 ... arg-n )) [color=red]<list-1>[/color] [color=magenta]<list-2>[/color] ... [color=purple]<list-n>[/color])

([color=blue]vl-some[/color] '([color=blue]predicate-function[/color] ( arg-1 ... arg-n )) [color=red]'(a1 a2 ... aN1)[/color] [color=magenta]'(b1 b2 ... bN2)[/color] ... [color=purple]'(z1 z2 ... zNn)[/color])

([color=blue]predicate-function[/color] [color=red]a1[/color] [color=magenta]b1[/color] ... [color=purple]z1[/color])
([color=blue]predicate-function[/color] [color=red]a2[/color] [color=magenta]b2[/color] ... [color=purple]z2[/color])
...
([color=blue]predicate-function[/color] [color=red]aN1[/color] [color=magenta]bN2[/color] ... [color=purple]zNn[/color]) [color=green];; until min(N1,N2,...,Nn) or predicate-function returns non-nil. [/color]

Posted

Thanks LEE

 

ive tried to write a function that take a list of values and search for a match in a nasted list

i belive i could do it with lots of code with 2 whiles but im sure you have a better way to do it with maocar and lambda

 

(setq suspect '(b1 c2)

(setq lst '((a1 a2) (b1 b2) (c1 c2)))

;_1. get element x in suspect list
;_2. search for its occurance in all sublist of lst
;_3. if it were found
; 4. _print and exit
;_5. if it werent found
;_6. start over again with the next elemt in the suspect list

Posted

hi again

 

this is what i could write by myself. i get bad argument error

 

(setq suspect '(b1 c2))
(setq lst '((a1 a2) (b1 b2) (c1 c2)))
   (if (equal (vl-some '(lambda ( x ) (vl-some '(lambda (y) (member x y))lst))suspect) nil)
           (princ "NO Match")
            (princ "Yes....the suspect has been matched")
     )

Posted

Your code looks correct to me, though, I would reverse the if statement:

(setq itm '(a3 z1 b1 c2)
     lst '((a1 a2) (b1 b2) (c1 c2))
)
(if (vl-some '(lambda ( x ) (vl-some '(lambda ( y ) (member x y)) lst)) itm)
   (princ "\nMatch found.")
   (princ "\nNo Match.")
)

Posted

hay LEE

 

i was thinking [ if ] expects only T or nil..now your are assing it a non-nil value

how come?

Posted
I was thinking [ if ] expects only T or nil..now your are assing it a non-nil value

 

In AutoLISP, any non-nil value is considered true.

Posted

trying to write the evaluation of this but get an error, any idea way?

 

(listsMatcher '(a2 a5) '((a1 a2) (b1 b2) (c1 c2)))
             
     
(defun listsMatcher (itm lst)
 (setq x 0 y 0)
   (while (< y (length y))
     (while(< x (length x))
(if
    (member (nth y itm) (nth x lst))
        (princ "\nMatch found.")
	(progn
           (princ "\nNo Match.")
	   (setq x (1+ x))
	)
       )
     )
     (setq y (1+ y))
 )
)

Posted

hay

 

this is what i manag to write,

 

lord...how many lines of code is save with mapcar ad lambda

 

;;;******************************************************************;;;
;;;--------------------------------------------------------------------;
;;;  Function: listsMatcher                                            ;
;;;--------------------------------------------------------------------;
;;;  Description: compares lists for the same occurances. first list   ;
;;;               is 1 level deep list while the second is 2 level deep;
;;;  Argument   : itm - 1 level deep list                              ;
;;;             : lst - 2 level deep list                              ;
;;;  Return     : T if at least one match found, nil if none           ;
;;;--------------------------------------------------------------------;


(setq itm '(b1 c3)
     lst '((a1 a2) (b1 b2) (c1 c2))
)
(listsMatcher itm lst)


(defun listsMatcher (itm lst)
(if (vl-some '(lambda ( x ) (vl-some '(lambda ( y ) (member x y)) lst)) itm)
    T
    nil
)
 )



             
     
(defun listsMatcher_EVAL (itm lst/ flag)
 (setq x 0 y 0 flag nil)
   (while (< y (length itm))
     (while(< x (length lst))
(if
    (member (nth y itm) (nth x lst))
        (progn
           (setq x 10 y 10)
	   (setq flag T)
	 )
	(progn
           (setq x (1+ x))
	   (setq flag nil)
	   
	)
       )
     )
     (setq y (1+ y))
     (setq x 0)
 )
 flag
)
    

 

2 things about the evaluation,

i use (setq x 10 y 10) when a match is found in order to quite, is that right thing to do?

i use flag to keep the boolean answer, is that right thing to do?

 

Thanks

Shay

Posted

Since any non-nil value in AutoLISP is considered true, this:

(defun listsMatcher (itm lst)
   (if (vl-some '(lambda ( x ) (vl-some '(lambda ( y ) (member x y)) lst)) itm)
       T
       nil
   )
 )

Can become simply:

(defun listsMatcher ( itm lst )
   (vl-some '(lambda ( x ) (vl-some '(lambda ( y ) (member x y)) lst)) itm)
)

i use (setq x 10 y 10) when a match is found in order to quite, is that right thing to do?

 

If you are intending this to be a generic function, you cannot guarantee that the length of the supplied list will not exceed 10 items.

 

i use flag to keep the boolean answer, is that right thing to do?

 

If a boolean return is specifically required, however, as stated above, this is not necessary since any non-nil value in AutoLISP is considered true.

 

Consider perhaps:

(defun listsmatcher2 ( itm lst / rtn x y )
   (while (and (null rtn) (setq x (car itm)))
       (setq tmp lst)
       (while (and (null rtn) (setq y (car tmp)))
           (setq rtn (member x y)
                 tmp (cdr tmp)
           )
       )
       (setq itm (cdr itm))
   )
   rtn
)

Or, recursively:

(defun listsmatcher3 ( itm lst )
   (   (lambda ( recurse ) (recurse itm lst lst))
       (lambda ( itm tmp lst )
           (cond
               (   (null itm) nil)
               (   (null lst) (recurse (cdr itm) tmp tmp))
               (   (member (car itm) (car lst)))
               (   (recurse itm tmp (cdr lst)))
           )
       )
   )
)

Posted

If you are intending this to be a generic function, you cannot guarantee that the length of the supplied list will not exceed 10 items.

how can i guarantee it?
Posted
(if (< (length lst) 10)
   ...

and what if not?

Posted
and what if not?

 

I don't understand your question.

Should the function return nil if the list contains 10 or more items?

Why the restriction to 10 items?

Posted
and what if not?

 

Me no comprendé.

 

Too ambiguous. :(

  • 6 months later...
Posted

This means that the condition will be satisfied as long as the length of lst is less than 10.

If lst is less than ten, do the THEN, if not do the ELSE, of the (if) statement.

 

If the length of lst is greater than or equal to ten then you'd simply write the conditional like

(if (>= (length lst) 10)
  ...
);if

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...