See the top rated post in this thread. Click here

Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: replace element in specific position of list

  1. #1
    I could stop if I wanted to
    Join Date
    2012-11
    Location
    Brisbane, Australia
    Posts
    239
    Login to Give a bone
    0

    Default replace element in specific position of list

    Hey All

    I have a very specific situation where I need to replace items in a list relative to its location in the list.
    So I want to be able to update each elements data without modifying their location in the list.
    the list will also be of varying length so it needs to be somewhat dynamic.

    something like:
    (setq (nth x1 list1) LatestData)
    where x1 will be on a counter

    Cheers

  2. #2
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,100
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Would the SUBST function work for your needs?
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  3. #3
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Are we talking about unique dotted pairs? Hard to answer without seeing an example list.

  4. #4
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    1

    Default Re: replace element in specific position of list


  5. #5
    I could stop if I wanted to
    Join Date
    2012-11
    Location
    Brisbane, Australia
    Posts
    239
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Lee-Mac's function will work perfectly.
    Thanks for the link Tom.

    just FYI Opie, I originally tried the subst but the due to all the lists starting blank, eg ( () () () () ).
    the subst function populates the data into all lists. other than that it would have been perfect.

    Cheers for all your help fellas.

  6. #6
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    1

    Default Re: replace element in specific position of list

    I thought it might be useful to create a set of functions for manipulating Lists.

    So I just wrote these functions.

    Any ideas for other functions?

    P=


    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Basic List Manipulation Functions
    ; Written By: Peter Jamtgaard Copyright 2015
    ;___________________________________________________________________________________________________________|
    
    (defun C:ListFunctionTest (/ lstOfValues)
     (and
      (setq lstOfValues (list "A" "B" "C" "D" 1 2 3 4))
      (setq lstOfValues (listinsert 4 lstOfValues 0))
      (princ "\n")
      (princ lstOfValues)
      (setq lstOfValues (listremove 4 lstOfValues))
      (princ "\n")
      (princ lstOfValues)
      (setq lstOfValues (listmoveup  3 lstOfValues))
      (princ "\n")
      (princ lstOfValues)
      (setq lstOfValues (listmovedown 2 lstOfValues))
      (princ "\n")
      (princ lstOfValues)
      (setq lstOfValues (listreplace 0 lstOfValues "Z"))
      (princ "\n")
      (princ lstOfValues)
      
     )
    )
    
    
    ; Function to add index number to front of value or sublist
    ;___________________________________________________________________________________________________________|
    
    (defun ListIndex (lstOfSublists / intCount )
     (setq intCount -1)
     (mapcar '(lambda (X)(cons (setq intCount (1+ intCount)) X)) lstOfSublists)
    )
    
    ; Function to insert a Value into a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
    ;___________________________________________________________________________________________________________|
    
    (defun ListInsert (intItem lstPrimary Value / lstIndexed lstSublist)
     (if (and
          (or intItem
              (setq intItem (length lstPrimary))
          )
          (setq lstIndexed (ListIndex lstPrimary))
          (setq lstSublist (cons (- intItem 0.5) Value))
          (setq lstIndexed (reverse (cons lstSublist (reverse lstIndexed))))
          (setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
         )
      (mapcar 'cdr lstIndexed)
      lstPrimary
     )
    )
    
    ; Function to move a Value down in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
    ;___________________________________________________________________________________________________________|
    
    (defun ListMoveDown (intItem lstPrimary / lstIndexed)
     (if (and
          (< -1 intItem (- (length lstPrimary) 2))
          (setq lstIndexed (ListIndex lstPrimary))
          (setq Value      (cdr (assoc intItem lstIndexed)))
          (setq lstIndexed (subst  (cons (+ intItem 1.5) Value) (assoc intItem lstIndexed) lstIndexed))
          (setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
         )
      (mapcar 'cdr lstIndexed)
      lstPrimary
     )
    )
    
    ; Function to move a Value up in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
    ;___________________________________________________________________________________________________________|
    
    (defun ListMoveUp (intItem lstPrimary / lstIndexed)
     (if (and
          (< 0 intItem (1- (length lstPrimary)))
          (setq lstIndexed (ListIndex lstPrimary))
          (setq Value      (cdr (assoc intItem lstIndexed)))
          (setq lstIndexed (subst  (cons (- intItem 1.5) Value) (assoc intItem lstIndexed) lstIndexed))
          (setq lstIndexed (SortListOfSublistsByItem lstIndexed 0))
         )
      (mapcar 'cdr lstIndexed)
      lstPrimary
     )
    )
    
    ; Function to remove a Value from a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
    ;___________________________________________________________________________________________________________|
    
    (defun ListRemove (intItem lstPrimary / lstIndexed lstSublist)
     (if (and
          (setq lstIndexed (ListIndex lstPrimary))
          (setq lstSublist (assoc intItem lstIndexed))
          (setq lstIndexed (vl-remove lstSublist lstIndexed))
         )
      (mapcar 'cdr lstIndexed)
      lstPrimary
     )
    )
    
    ; Function to replace a Value down in a list (lstPrimary) at a position (intItem) - Zero (0) is the first item
    ;___________________________________________________________________________________________________________|
    
    (defun ListReplace (intItem lstPrimary Value / lstIndexed lstSublist)
     (if (and
          (setq lstIndexed (ListIndex lstPrimary))
          (setq lstSublist (assoc intItem lstIndexed))
          (setq lstIndexed (subst  (cons intItem Value) lstSublist lstIndexed))
         )
      (mapcar 'cdr lstIndexed)
      lstPrimary
     )
    )
    
    ; General Function for sorting a list of sublists into ascending order of a specified item in the sublists:
    ;___________________________________________________________________________________________________________|
    
    (defun sortListofSublistsbyItem (lstOfSublists intItem)
     (vl-sort lstOfSublists '(lambda (X Y) (< (nth intItem X) (nth intItem Y))))
    )
    
    (vl-load-com)
    Attached Files Attached Files
    AutomateCAD

  7. #7
    I could stop if I wanted to
    Join Date
    2012-11
    Location
    Brisbane, Australia
    Posts
    239
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Thanks Peter, these look useful.
    One thing I would like to raise though is point lists.
    I have been working with point lists lately and have found many default functions do not work as desired when comparing point lists.
    I believe the problem arises from checking 3 points of information.

  8. #8
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    1

    Default Re: replace element in specific position of list

    When comparing points I would use a Fuzz factor.

    Code:
    (defun PointsAreEqual (lstPoint1 lstPoint2 sngFuzz)
     (apply 'and (mapcar '(lambda (X Y)(equal X Y sngFuzz)) lstPoint1 lstPoint2))
    )
    
    (pointsareequal (list 0.0 0.0 0.01) (list 0.0 0.0 0.0) 0.011)
    
    ;Returns T
    
    (pointsareequal (list 0.0 0.0 0.01) (list 0.0 0.0 0.0) 0.009)
    
    ;Returns nil
    AutomateCAD

  9. #9
    I could stop if I wanted to
    Join Date
    2012-11
    Location
    Brisbane, Australia
    Posts
    239
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Okay I've got another one for you Peter.
    Hopefully it is a legitimate question and not the lack of sleep talking.
    I want to check if a point is contained within a list that contains lists of points
    eg:
    ( ((1 1 1) (2 2 2) (3 3 3)) ((4 4 4) (5 5 5) (6 6 6)) ((7 7 7) (8 8 8 ) (9 9 9)) )

    the number of lists will vary in number.
    the number of points contaiend within will also vary in number.

    I just need a true false if a single point is contained in any of the lists.
    Last edited by LSElite; 2015-09-28 at 11:21 AM. Reason: Wreched Smilies

  10. #10
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: replace element in specific position of list

    Something like this.

    P=

    Code:
    (defun C:IsStart (/ lstOfSublists lstPoint lstPoints lstReturn)
     (setq lstOfSublists '(((1.0 1 1) (2 2 2) (3 3 3) (4 4 4) ) ((5 5 5) (6 6 6)) ((7 7 7) (8 8 8 ) (9 9 9)) ))
     (setq lstPoint      (list 2.0001 2 2))
     (isPointInList lstOfSublists)
     (apply 'or (mapcar '(lambda(X)(ArePointsEqual X lstPoint 0.001)) lstReturn))
    )
    
    (defun IsPointInList (lstOfSublists)
     (foreach lstPoint lstOfSublists
      (if (IsPoint lstPoint)
       (setq lstReturn (cons lstPoint lstReturn))
       (if (and (= (type lstPoint) 'LIST)
                (> (length lstPoint) 0)
           )
        (isPointInList lstPoint)
       )
      )
     )
    )
    
    (defun IsNumber (sngNumber)
     (or 
      (= (type sngNumber)'REAL)
      (= (type sngNumber)'INT)
     )
    )
    
    (defun IsPoint (lstPoint)
     (and
      (> (length lstPoint) 0)
      (apply 'and (mapcar 'isnumber lstPoint))
     )
    )
    
    (defun ArePointsEqual (lstPoint1 lstPoint2 sngFuzz)
     (apply 'and (mapcar '(lambda (X Y)(equal X Y sngFuzz)) lstPoint1 lstPoint2))
    )
    Last edited by peter; 2015-10-04 at 01:22 AM.
    AutomateCAD

Page 1 of 2 12 LastLast

Similar Threads

  1. vl-position not working on point list
    By tufofi in forum AutoLISP
    Replies: 4
    Last Post: 2012-01-20, 05:41 PM
  2. Replace a specific line of text within a file
    By ccowgill in forum AutoLISP
    Replies: 15
    Last Post: 2009-06-15, 05:21 PM
  3. Make family element visibility settings view-specific
    By revit.wishlist1942 in forum Revit Architecture - Wish List
    Replies: 3
    Last Post: 2008-05-29, 02:35 PM
  4. Adding a value to position in a list
    By dkennard in forum AutoLISP
    Replies: 3
    Last Post: 2007-06-29, 01:03 PM
  5. CUI specific Wish List Please?
    By jguest82179 in forum AutoCAD CUI Menus
    Replies: 5
    Last Post: 2005-09-08, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •