+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
  1. #1
    Junior Member
    Discipline
    Civil
    Using
    AutoCAD 2014
    Join Date
    Mar 2017
    Posts
    12

    Default Put mass selected integers in ascending order starting from a given integer

    Registered forum members do not see this ad.

    Hello,

    Let's say we have a group of integers as text in dwg.
    How would we put them in in ascending order by one(+1) starting from a given integer?
    Let me give an example to make it clear. Let's say we have the texts 1 2 5 5 5 8 9 11 11 15 and the given start integer is 6. We select them all together and the range in selection set is random. Let's say 5 2 8 15 1 5 5 11 9 11.
    Now, we want to put them in ascending order by one starting from number 6 replacing the "old" range with the new. In the end we must have this:
    6 7 8 8 8 9 10 11 11 12.
    That is 6(in place of 1), 7(in place of 2), 8(5), 8(5), 8(5), 9(, 10(9), 11(11), 11(11), 12(15). I hope it is clear.
    How would we do that in an efficient way?

    Thank you in advance,
    Kostas

  2. #2
    Junior Member
    Discipline
    Civil
    Using
    AutoCAD 2014
    Join Date
    Mar 2017
    Posts
    12

    Default

    9(, = 9 ( 8 )

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,086

    Default

    Consider the following code:
    Code:
    (defun renumberlist ( num lst )
        (setq lst (vl-sort lst '(lambda ( a b ) (< (atoi a) (atoi b))))
              num (1- num)
        )
        (mapcar '(lambda ( a b ) (if (= a b) (itoa num) (itoa (setq num (1+ num))))) (cons nil lst) lst)
    )
    Example:
    Code:
    _$ (renumberlist 6 '("5" "2" "8" "15" "1" "5" "5" "11" "9" "11"))
    ("6" "7" "8" "8" "8" "9" "10" "11" "11" "12")
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  4. #4
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    838

    Default

    Quote Originally Posted by Lee Mac View Post
    Consider the following code:
    Its impressive how fast you understand peoples tasks/problems.

  5. #5
    Junior Member
    Discipline
    Civil
    Using
    AutoCAD 2014
    Join Date
    Mar 2017
    Posts
    12

    Default

    Thank you Lee Mac! I will try to import your code to my routine. If I don't manage, I will come back to you. (It's very possible because your coding is in much higher level than mine). Thanks!

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,086

    Default

    Quote Originally Posted by Grrr View Post
    Its impressive how fast you understand peoples tasks/problems.
    Thanks

    Quote Originally Posted by tsotzo View Post
    Thank you Lee Mac! I will try to import your code to my routine. If I don't manage, I will come back to you. (It's very possible because your coding is in much higher level than mine). Thanks!
    You're welcome tsotzo. My code demonstrates one possible method of ordering & renumbering a set of text values, however, assuming you are looking to then modify the text content, you will need to retain the entity name of the text object during the sorting & renumbering process - I'm happy to demonstrate this if you get stuck.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,086

    Default

    Just for fun, here's a recursive function accepting a sorted list to produce the same result:
    Code:
    (defun renum ( num lst )
        (if lst (cons (itoa num) (renum (if (= (car lst) (cadr lst)) num (1+ num)) (cdr lst))))
    )
    Example:
    Code:
    _$ (renum 6 (vl-sort '("5" "2" "8" "15" "1" "5" "5" "11" "9" "11") '(lambda ( a b ) (< (atoi a) (atoi b)))))
    ("6" "7" "8" "8" "8" "9" "10" "11" "11" "12")
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  8. #8
    Junior Member
    Discipline
    Civil
    Using
    AutoCAD 2014
    Join Date
    Mar 2017
    Posts
    12

    Default

    That's right!

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,086

    Default

    OK, I couldn't resist

    Code:
    (defun c:renum ( / a b i l n s x y z )
        (initget 4)
        (setq n (1- (cond ((getint "\nSpecify starting number <1>: ")) (1))))
        (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "~*[~0-9]*"))))
            (progn
                (repeat (setq i (sslength s))
                    (setq x (entget (ssname s (setq i (1- i))))
                          a (cons (assoc -1 x) a)
                          b (cons (atoi (cdr (assoc 1 x))) b)
                    )
                )
                (setq i (vl-sort-i b '<)
                      l (mapcar '(lambda ( n ) (nth n b)) i)
                )
                (mapcar '(lambda ( x y z ) (entmod (list (nth z a) (cons 1 (itoa (if (= x y) n (setq n (1+ n)))))))) (cons nil l) l i)
            )
        )
        (princ)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  10. #10
    Junior Member
    Discipline
    Civil
    Using
    AutoCAD 2014
    Join Date
    Mar 2017
    Posts
    12

    Default

    Registered forum members do not see this ad.

    & & . Lee Mac, when I first read your message I laughed with my heart and then... wow... I have no words...! You are unbelievable! You know, I had written more than double of code lines than you, and I wasn't even in the middle! What can I say... Thanks man!! You know, what's impressive is not only that you solve the problem, but the way that you do it (and the speed of course ). Thanks again!

Similar Threads

  1. Lisp for Auto numbering in ascending order while inserting block attributes
    By gmmdinesh in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 26th Sep 2016, 11:21 am
  2. Finding Integers
    By b_clive in forum AutoCAD General
    Replies: 1
    Last Post: 9th Dec 2010, 10:46 am
  3. Numbering Ascending order
    By Kat in forum AutoCAD General
    Replies: 4
    Last Post: 18th Jan 2010, 02:13 pm
  4. sort ascending using VBA
    By priyanka_mehta in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 9th Oct 2009, 12:59 pm

Tags for this Thread

Bookmarks

Posting Permissions

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