0

Here's the problem in the code:

    (foreach n l_pt
        (write-line
            (strcat "sommet" str_sep
                (itoa (setq (nbs (+1 nbs )))) str_sep
                (rtos (car n) 2 2) str_sep
                (rtos (cadr n) 2 2) str_sep
                (strcat "2") str_sep
                (strcat "borne")
            )
            f_open
        )
    )
    ;;(write-line "" f_open)

I have these kind of files in output:

Sommets;;

Type;Num;X;Y;Precison;Nature

sommet;1;1532292.16;3214140.11;2;borne

sommet;2;1532287.08;3214140.60;2;borne

sommet;1;1532291.45;3214136.43;2;borne

sommet;2;1532286.50;3214135.87;2;borne

sommet;1;1532287.08;3214140.60;2;borne

As you've guessed, the problematic part is the "Num" which is not working as intended, incrementing.

I've understood that it's this line: " (itoa (setq (nbs (+1 nbs )))) str_se" not working as intended, but I don't know why. I've tried to switch it to (setq (nbs (+ 1 nbs))), but it doesn't works either.

Do you have any ideas from where it's coming?

Complete code

8
  • Does (+ 1 nbs) do exactly the same thing as (+1 nbs)? Commented Jul 13, 2016 at 17:07
  • 2
    Forms in languages in the Lisp family are generally written as (operator arg1 arg2 ...). In the case of setq, the assignment operator, the argument is order is (setq <place> <new value>). That means that (setq (nbs (+1 nbs))) is wrong, and should be (setq nbs (+ 1 nbs)). Commented Jul 13, 2016 at 17:12
  • Good catch, @Joshua. When you're used to languages that take function params in (), it's easy to miss this kind of thing. You might want to post that as an answer. (As an aside, it's possible that +1 is a defined incrementer function and that (+1 nbs) returns the same value as (+ 1 nbs). However the latter form is clearly correct.) Commented Jul 13, 2016 at 17:32
  • 1
    @Feldur Sure, here is it : link Commented Jul 17, 2016 at 14:17
  • 1
    Can you check what happens with a minimal example like this one? (defun test (/ counter) (foreach n '(a b c) (write-line (itoa (setq counter (+ 1 counter)))))) Commented Jul 17, 2016 at 15:42

2 Answers 2

1

I think the problem is that you're just off by one parenthesis. Or that you need to initialize nbs outside the repeat. It depends on how you want it to work.

It looks like your code (greatly simplified) is this:

;; Stuff before the problem
(repeat (sslength js)
    ;; (setq....)
    (setq nbs 0)
    ;;; Lots of stuff
    (foreach n l_pt
        ;; The printing code including...
        (setq nbs (+ 1 nbs))
    )
)

As you can see, you're setting nbs to 0 each time you go through the repeat loop and then adding 1 to it later.

It looks like you wanted to separate those loops - repeat until finished calculating the information, and then foreach to print out the whole list l_pt.

As it stands, I think you're only appending 1 line of information to l_pt each time through the big repeat loop and only printing that one item from the foreach statement.

Moving a parenthesis would give you this:

(repeat (sslength js)
    ;;...
    (setq nbs 0)
    ;;...
)
(foreach n l_pt
    ;;...
    (setq nbs (+ 1 nbs))
)

You could also place the (setq nbs 0) outside the repeat loop. That would probably be best either way so that it's clear what you're doing and there's no need to initialize it 5+ times...

Also, this shows why it's a good idea to clearly separate your variable assignments. They can get lost at the end of a long (setq) statement:

;; Do this,
(setq sentence "This is one or more variable assignments."
      number 200)

;; or this,
(setq sentence "This is one or more variable assignments.")
(setq number 200)

;; not this.
(setq sentence "This is one or more variable assignments." number 200)

Anyway, either of those options should get your counter nbs working again.

Hope that helps!

P.S. Just to help with the (+1 nbs) discussion in the comments above, the following are all correct and equivalent in AutoLISP:

(+ 1 nbs)
(+ nbs 1)         ;; Order is not important for addition
(+ nbs 1 0 0 0)   ;; Any number of values can be added
(1+ nbs)          ;; The typical way to increment a value
                  ;; (1+ is a function that returns the value + 1)
Sign up to request clarification or add additional context in comments.

Comments

0

It must be something peculiar to AutoLISP. I say that because this code (adapted to Common Lisp as you'll see)

(defun foo (l_pt)
  (let ((str_sep ";")
        (nbs 0))

    (mapcar #'(lambda (n) (write-line (strcat (list "sommet" str_sep
                                                    (itoa (setq nbs (+ 1 nbs))) str_sep
                                                    (rtos (car n) 2 2) str_sep
                                                    (rtos (cadr n) 2 2) str_sep
                                                    (strcat "2") str_sep
                                                    (strcat "borne")))
                                      ))
            l_pt)))

(defun rtos (a b c) (strcat a))

(defun strcat (string-designators)
  (with-output-to-string (strcat)
    (cond ((listp string-designators)
           (mapcar #'(lambda (s) (princ s strcat)) string-designators))
          (t (princ string-designators strcat)))))

(defun itoa (i) (write-to-string i))

for this expression:

(foo '((x y) (a b) (aa bb)))

produces this output:

sommet;1;X;Y;2;borne
sommet;2;A;B;2;borne
sommet;3;AA;BB;2;borne

Admittedly, I had to change many things to accommodate elements of AutoLISP that aren't in Common Lisp, perhaps the most significant of which is I didn't write an equivalent of foreach (because there's no way to know I'd replicate its idiosyncrasies), but nevertheless it at least appears setq is doing what you expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.