Author Topic: (challenge) grab item from a nested list  (Read 3330 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
(challenge) grab item from a nested list
« on: July 22, 2005, 12:54:41 PM »
and return it as though it were in a numbered matrix
example:
Code: [Select]

(
 ("Baseline" "Center" "Right" "Aligned" "Middle" "Fit")
 ("BLeft" "BCenter" "BRight")
 ("MLeft" "MCenter" "MRight")
 ("TLeft" "TCenter" "TRight")
)

User supplies "Baseline" and the return value is '(0 0)
User supplies "MCenter" and the return value is '(2 1)
The first number in the return list is the position of the list containing the item and the second number in the return list is the position of the item within that list, both starting at zero.

How would you rate this challenge? Beginner, Intermediate, Expert.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) grab item from a nested list
« Reply #1 on: July 22, 2005, 01:37:01 PM »
intermediate
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

daron

  • Guest
(challenge) grab item from a nested list
« Reply #2 on: July 22, 2005, 01:54:06 PM »
I'm thinking so too as I work this. It seems like fun.

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
(challenge) grab item from a nested list
« Reply #3 on: July 22, 2005, 02:54:05 PM »
I would say that its more in between beginer and intermediate.

I ran thru an initial test/thoughts; here they are.

Code: [Select]
(defun listpos (item lst)
  (cond
    ((member item lst)
       (- (length lst)
        (length (member item lst))))
    (T 'nil)))

(defun maintest ()
 
  ;; this just sets up some variables for a test run.
  (setq mylst '((1 2 3) (1 2 3) (1 2 3 4)))
  (setq cntr (1- (length mylst)))
 
  ;; Itterate thru the list members untill you find the item
  (while (eq (setq n (listpos 4 (nth cntr mylst))) 'nil)
    (setq cntr (1- cntr))
    )
 
  ;;return the counter (the sublist) and the item postion
  (cons (1+ cntr) (1+ n))
 
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) grab item from a nested list
« Reply #4 on: July 22, 2005, 03:04:36 PM »
Here is my take.
Returns nil is not found.

Code: [Select]
(defun get_index ($key lst / i1 i2 loop)
  (setq i1 0 loop t)
  (while (and loop (< i1 (length lst)))
    (setq i2 0)
    (while (and loop (< i2 (length (nth i1 lst))))
      (if (= $key (nth i2 (nth i1 lst)))
        (setq loop nil)
      )
      (setq i2 (1+ i2))
    )
    (setq i1 (1+ i1))
  )
  (if loop nil (list i1 i2))
)



Code: [Select]
(defun c:test ()
  (setq result (get_index "TCenter"
                          '(("Baseline" "Center" "Right" "Aligned" "Middle" "Fit")
                            ("BLeft" "BCenter" "BRight")
                            ("MLeft" "MCenter" "MRight")
                            ("TLeft" "TCenter" "TRight")
                           )
               )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) grab item from a nested list
« Reply #5 on: July 22, 2005, 03:26:45 PM »
The previous one returned pointers base 1,
this routine returns pointers base 0.
Thanks to Louis for the vl-some example. :)

Code: [Select]
(defun get_index ($key lst / lst2 pos result)
  (if (vl-some '(lambda (lst2 / pos)
                  (if (setq pos (vl-position $key lst2))
                    (setq result (list pos lst2))
                  )
                )
               lst
      )
    (setq result (list (vl-position (cadr result) lst) (car result)))
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) grab item from a nested list
« Reply #6 on: July 22, 2005, 04:00:07 PM »
Here is yet another
Code: [Select]
(defun findpos (testval testlist / lst m n parse )
  (setq
    parse (mapcar
   '(lambda (x)
      (1-
(length
  (member
    testval
    (reverse x)
  )
)
      )
    )
   testlist
 )
  )
  (setq m 0)
  (repeat (length parse)
    (if (> (setq n (nth m parse)) -1)
      (setq lst (list m n))
    )
    (setq m (1+ m))
    lst
  )
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

daron

  • Guest
(challenge) grab item from a nested list
« Reply #7 on: July 22, 2005, 05:08:13 PM »
Cool stuff guys. Here's what I came up with:
Code: [Select]
;;;-------------------------------Matrix<-List----------------------------------;
;;; Purpose: This function will take a nested list of any supplied values, ;
;;; and compare it with an item to search for and return the position ;
;;; of that item as though it were in a matrix grid ;
;;;-----------------------------------------------------------------------------;
;;; example: (Matrix<-List ;
;;; 1 ;
;;; (list (list "STRING") ;
;;;       (list "Black" "TRight") ;
;;;       (list "Made" 1) ;
;;; ) ;
;;;    ) ;
;;; example return: (2 1) ;
;;;_____________________________________________________________________________;
(defun Matrix<-List (value matrix     / whichlist
    itemposition     isit code73
    posilength code72
   )
     (setq whichlist 0)
     (setq itemposition
      (mapcar '(lambda (x)
    (if (not (setq isit (member value x)))
 nil
 (setq code73 (- (length x)
 (length isit)
      )
 )
    )
)
      matrix
      )
     )
     (foreach x itemposition
(if (numberp x)
    (setq posilength (length (member x itemposition)))
)
  );length of position of itemposition
     (cond (posilength
  (setq code72 (- (length itemposition) posilength))
  (setq return (list code72 code73))
 )
 (t
  (setq return
    (princ
 (strcat
      "\nThe value supplied "
      value
      " did not match any items in the list supplied."
 )
    )
  )
 )
     )
     return
)


I don't think I'd say this would be a beginner challenge. Definately Intermediate. I think that because I wouldn't know of too many beginners that could figure it out let alone dream up why you'd want to even do this. As for my plans for it, it keeps me from having to use cond in converting dxf 72 and 73 values from text strings. Makes the code read so much better. I'm sure there'll be many more areas where it can be put to practical use. Matrices seem to be really misunderstood and therefore, not used very often.