Author Topic: Adding Numbers  (Read 8119 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #15 on: August 20, 2015, 05:33:58 PM »
One more error that I have discovered is that if the user removes an attribute from the selection set, then readds the block by say selecting another attribute, it will also include the removed attribute value in the total. I am not sure how to use ssnamex to determine if the pick point or crossing window was used to remove or add an object, any ideas?
« Last Edit: August 20, 2015, 05:45:35 PM by cmwade77 »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Adding Numbers
« Reply #16 on: August 20, 2015, 07:47:43 PM »
Try this: (atof "100 ABC") .. if you're just adding numbers it should be fine.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #17 on: August 20, 2015, 08:03:46 PM »
Try this: (atof "100 ABC") .. if you're just adding numbers it should be fine.
Interesting, I will have to play more with it, I was getting errors on some strings.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #18 on: August 21, 2015, 11:55:56 AM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Adding Numbers
« Reply #19 on: August 21, 2015, 12:09:39 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ?
(read "test 100 test") returns a symbol of TEST .. maybe I'm missing the point?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #20 on: August 21, 2015, 12:15:07 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ? (read "test 100 test") returns a symbol of TEST. Maybe i'm missing the point?
But if you look what this function is doing is pulling the numbers out of the string and making sure they are in the correct order, then using read on that string to get the result.

So try this:
Code: [Select]
(defun che_Str2Num (str / test numlst lst)
(if (= (substr str 1 1) ".")
(setq Str (strcat "0" str))
)
(setq test (list 46 48 49 50 51 52 53 54 55 56 57))
(setq charlst (vl-string->list str))
(foreach x charlst
(if (member x test)
(setq lst (cons x lst))
(setq lst (cons 32 lst))
)
)
(read (vl-list->string (reverse lst)))
)
(che_str2num "TEST 100 TEST")

And you will see that it will return 100 and not TEST.

The other key to this, which is necessary for another routine that the function gets used in is that this keeps the correct number of decimal places.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Adding Numbers
« Reply #21 on: August 21, 2015, 12:26:35 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ? (read "test 100 test") returns a symbol of TEST. Maybe i'm missing the point?
But if you look what this function is doing is pulling the numbers out of the string and making sure they are in the correct order, then using read on that string to get the result.

So try this:
Code: [Select]
(defun che_Str2Num (str / test numlst lst)
      (if (= (substr str 1 1) ".")
         (setq Str (strcat "0" str))
      )
      (setq test (list 46 48 49 50 51 52 53 54 55 56 57))
      (setq charlst (vl-string->list str))
      (foreach x charlst
         (if (member x test)
            (setq lst (cons x lst))
            (setq lst (cons 32 lst))
         )
      )
      (read (vl-list->string (reverse lst)))
   )
(che_str2num "TEST 100 TEST")

And you will see that it will return 100 and not TEST.

The other key to this, which is necessary for another routine that the function gets used in is that this keeps the correct number of decimal places.
That's why I said "but you're using the che_Str2Num function". Your comparison here is comparing apple to oranges.  :P
This is all I was getting at:
Code - Auto/Visual Lisp: [Select]
  1. (setq str "test .100 test")
  2.        (mapcar '(lambda (x)
  3.              (if   (or (= x 46) (<= 48 x 57))
  4.                (chr x)
  5.                ""
  6.              )
  7.            )
  8.           (vl-string->list str)
  9.        )
  10.      )
  11. )
  12. (if str
  13.   (progn ;; Number
  14.     (atof str)
  15.     ;; Boom
  16.     (read str)
  17.     ;; No Boom
  18.     (read (strcat "0" str))
  19.   )
  20. )
:(
« Last Edit: August 21, 2015, 12:44:34 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Adding Numbers
« Reply #22 on: August 21, 2015, 12:42:22 PM »
Admittedly I haven't thoroughly read the entire thread, but where the 'che_str2num' function is concerned, perhaps my Parse Numbers function may be of help?

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #23 on: August 21, 2015, 07:04:35 PM »
Admittedly I haven't thoroughly read the entire thread, but where the 'che_str2num' function is concerned, perhaps my Parse Numbers function may be of help?

Ok, but your code doesn't seem to work with the string "test .100 test", it returns 100 instead of .100. In all fairness my routine presently crashes with this, but I am working on a fix.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #24 on: August 21, 2015, 07:55:01 PM »
I fixed the decimal point issues in my code and I now provide some form of visual feedback as to which attributes and pieces of text were included in the total. I am quite sure there is a lot that can be done to improve this code, but it seems to at least work for the time being. I believe that I have accounted for most errors, except the removing of attributes from the selection set, as I mentioned before. I wonder if Lee might already have a solution for how to tell if a point picked in a selection set was for adding or removing something?

The updated code is in the first post.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #25 on: August 21, 2015, 08:00:31 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ? (read "test 100 test") returns a symbol of TEST. Maybe i'm missing the point?
But if you look what this function is doing is pulling the numbers out of the string and making sure they are in the correct order, then using read on that string to get the result.

So try this:
Code: [Select]
(defun che_Str2Num (str / test numlst lst)
      (if (= (substr str 1 1) ".")
         (setq Str (strcat "0" str))
      )
      (setq test (list 46 48 49 50 51 52 53 54 55 56 57))
      (setq charlst (vl-string->list str))
      (foreach x charlst
         (if (member x test)
            (setq lst (cons x lst))
            (setq lst (cons 32 lst))
         )
      )
      (read (vl-list->string (reverse lst)))
   )
(che_str2num "TEST 100 TEST")

And you will see that it will return 100 and not TEST.

The other key to this, which is necessary for another routine that the function gets used in is that this keeps the correct number of decimal places.
That's why I said "but you're using the che_Str2Num function". Your comparison here is comparing apple to oranges.  :P
This is all I was getting at:
Code - Auto/Visual Lisp: [Select]
  1. (setq str "test .100 test")
  2.        (mapcar '(lambda (x)
  3.              (if   (or (= x 46) (<= 48 x 57))
  4.                (chr x)
  5.                ""
  6.              )
  7.            )
  8.           (vl-string->list str)
  9.        )
  10.      )
  11. )
  12. (if str
  13.   (progn ;; Number
  14.     (atof str)
  15.     ;; Boom
  16.     (read str)
  17.     ;; No Boom
  18.     (read (strcat "0" str))
  19.   )
  20. )
:(

Yeah, the problem with this method is it adds a 0, even if the first character isn't a ., which is a compatibility problem with other code that will eventually be incorporated into this routine.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Adding Numbers
« Reply #26 on: August 21, 2015, 08:46:13 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ? (read "test 100 test") returns a symbol of TEST. Maybe i'm missing the point?
But if you look what this function is doing is pulling the numbers out of the string and making sure they are in the correct order, then using read on that string to get the result.

So try this:
Code: [Select]
(defun che_Str2Num (str / test numlst lst)
      (if (= (substr str 1 1) ".")
         (setq Str (strcat "0" str))
      )
      (setq test (list 46 48 49 50 51 52 53 54 55 56 57))
      (setq charlst (vl-string->list str))
      (foreach x charlst
         (if (member x test)
            (setq lst (cons x lst))
            (setq lst (cons 32 lst))
         )
      )
      (read (vl-list->string (reverse lst)))
   )
(che_str2num "TEST 100 TEST")

And you will see that it will return 100 and not TEST.

The other key to this, which is necessary for another routine that the function gets used in is that this keeps the correct number of decimal places.
That's why I said "but you're using the che_Str2Num function". Your comparison here is comparing apple to oranges.  :P
This is all I was getting at:
Code - Auto/Visual Lisp: [Select]
  1. (setq str "test .100 test")
  2.        (mapcar '(lambda (x)
  3.              (if   (or (= x 46) (<= 48 x 57))
  4.                (chr x)
  5.                ""
  6.              )
  7.            )
  8.           (vl-string->list str)
  9.        )
  10.      )
  11. )
  12. (if str
  13.   (progn ;; Number
  14.     (atof str)
  15.     ;; Boom
  16.     (read str)
  17.     ;; No Boom
  18.     (read (strcat "0" str))
  19.   )
  20. )
:(

Yeah, the problem with this method is it adds a 0, even if the first character isn't a ., which is a compatibility problem with other code that will eventually be incorporated into this routine.
This seems to be a moving target. The title of the thread is "adding numbers". If you're parsing a number out of strings, adding a 0 in front and calling READ has no side effect other than avoiding your 'misplaced dot on input' error.
Try this:

(read "0.1")
(read "01000")


Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Adding Numbers
« Reply #27 on: August 22, 2015, 06:46:59 AM »
Admittedly I haven't thoroughly read the entire thread, but where the 'che_str2num' function is concerned, perhaps my Parse Numbers function may be of help?

Ok, but your code doesn't seem to work with the string "test .100 test", it returns 100 instead of .100. In all fairness my routine presently crashes with this, but I am working on a fix.

But ".100" is not a valid number as far as AutoLISP is concerned.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Adding Numbers
« Reply #28 on: August 22, 2015, 08:24:35 AM »
This is a very old lisp (2004+) that may be of some help.  Lee's parse number lisp is a good way to go.
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.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Adding Numbers
« Reply #29 on: August 24, 2015, 12:03:05 PM »
Ok, but (atof "test 100 test") returns 0 instead of 100. I don't plan on needing to be able to do this, but who knows what one of my users might do.
But you're using the "che_str2num" function ? (read "test 100 test") returns a symbol of TEST. Maybe i'm missing the point?
But if you look what this function is doing is pulling the numbers out of the string and making sure they are in the correct order, then using read on that string to get the result.

So try this:
Code: [Select]
(defun che_Str2Num (str / test numlst lst)
      (if (= (substr str 1 1) ".")
         (setq Str (strcat "0" str))
      )
      (setq test (list 46 48 49 50 51 52 53 54 55 56 57))
      (setq charlst (vl-string->list str))
      (foreach x charlst
         (if (member x test)
            (setq lst (cons x lst))
            (setq lst (cons 32 lst))
         )
      )
      (read (vl-list->string (reverse lst)))
   )
(che_str2num "TEST 100 TEST")

And you will see that it will return 100 and not TEST.

The other key to this, which is necessary for another routine that the function gets used in is that this keeps the correct number of decimal places.
That's why I said "but you're using the che_Str2Num function". Your comparison here is comparing apple to oranges.  :P
This is all I was getting at:
Code - Auto/Visual Lisp: [Select]
  1. (setq str "test .100 test")
  2.        (mapcar '(lambda (x)
  3.              (if   (or (= x 46) (<= 48 x 57))
  4.                (chr x)
  5.                ""
  6.              )
  7.            )
  8.           (vl-string->list str)
  9.        )
  10.      )
  11. )
  12. (if str
  13.   (progn ;; Number
  14.     (atof str)
  15.     ;; Boom
  16.     (read str)
  17.     ;; No Boom
  18.     (read (strcat "0" str))
  19.   )
  20. )
:(

Yeah, the problem with this method is it adds a 0, even if the first character isn't a ., which is a compatibility problem with other code that will eventually be incorporated into this routine.
This seems to be a moving target. The title of the thread is "adding numbers". If you're parsing a number out of strings, adding a 0 in front and calling READ has no side effect other than avoiding your 'misplaced dot on input' error.
Try this:

(read "0.1")
(read "01000")
Not really, it's just this particular subfunction gets reused elsewhere and has specific requirments, otherwise your suggestion would work quite well.