Better way for mapcar

Discussion in 'AutoCAD' started by BTO, Jul 7, 2004.

  1. BTO

    BTO Guest

    hello

    i'm looking for a better way to write this mapcar (or other advice or other
    way)
    my final result :

    (setq path_acad (strcat (vl-registry-read (strcat "HKEY_LOCAL_MACHINE\\"
    (vlax-product-key)) "ACADLOCATION") "\\"))
    (setq file_scr (open (strcat path_acad "macro.scr") "w"))
    (setq temp2 nil)
    (mapcar
    '(lambda (x)
    (if (not temp2)
    (setq temp2 x )
    (progn
    (write-line (strcat temp2 " " x ) file_scr )
    (setq temp2 nil )
    )
    )
    )
    (list "ATTDIA" "0" "ATTMODE" "1" "ATTREQ" "1" )
    )
    (close file_scr )

    Goal is to create a batch script (it works perfectly), but i want to replace
    my old :

    (write-line "BLIPMODE 0" fichier_scr )
    (write-line "COORDS 1" fichier_scr )
    (write-line "DELOBJ 1" fichier_scr )
    (write-line "DISPSILH 0" fichier_scr )
    (write-line "EDGEMODE 1" fichier_scr )
    (write-line "ELEVATION 0" fichier_scr )
    (write-line "EXPLMODE 1" fichier_scr )
    (write-line "FACETRES 0.5" fichier_scr )
    (write-line "FILLMODE 1" fichier_scr )
    (write-line "GRIDMODE 0" fichier_scr )
    (write-line "HIGHLIGHT 1" fichier_scr )
    (write-line "INDEXCTL 0" fichier_scr )
    etc..............

    ...I hope, i did not miss my copy-past

    Bruno Toniutti
    (sorry for my english level)
     
    BTO, Jul 7, 2004
    #1
  2. BTO

    zeha Guest

    experience with vl-member-if-not

    cheers
    Harrie
     
    zeha, Jul 7, 2004
    #2
  3. BTO

    Devin Guest

    BTO,

    personally I only use mapcar if I'm building a list. Or if it's really time
    critical. I think you could just use foreach, something like...

    (setq lst
    '(
    ("ATTDIA" "0")
    ("ATTMODE" "1")
    ("ATTREQ" "1")
    )
    )
    (foreach var lst
    (write-line (strcat (car var) " " (cadr var)) file_scr)
    )

    I think it's cleaner perhaps?
     
    Devin, Jul 7, 2004
    #3
  4. BTO

    Tom Smith Guest

    I'd probably keep the variables and values as association lists rather than
    stringing them together in one list.

    (mapcar
    '(lambda (alist)
    (write-line (strcat (car alist) " " (cdr alist)) file_scr))
    '(("ATTDIA" . "0") ("ATTMODE" . "1") ("ATTREQ" . "1")))

    Or you could use foreach as Devin said

    (foreach alist '(("ATTDIA" . "0") ("ATTMODE" . "1") ("ATTREQ" . "1"))
    (write-line (strcat (car alist) " " (cdr alist) ) file_scr )))
     
    Tom Smith, Jul 7, 2004
    #4
  5. BTO

    BTO Guest

    BTO,
    thx,

    yes it's cleaner,
    but if I "make" lst as you saw,
    mapcar only need something like :
    (mapcar '(lambda (x) (write-line (strcat (car x) " " (cadr x)) file_scr))
    lst )

    but i agree, in this case a "foreach" is cleaner.

    list as I typed is part of challenge ;)

    Bruno Toniutti
     
    BTO, Jul 7, 2004
    #5
  6. BTO

    BTO Guest

    thx
    i'm doing that, may be a more elegant result than my "temp2"...

    Bruno Toniutti
     
    BTO, Jul 7, 2004
    #6
  7. time critical ?
    question:
    is mapcar faster then foreach ???

    Cheers
    Dieter
     
    Dieter Berger, Jul 7, 2004
    #7
  8. BTO

    Devin Guest

    That's what I've read on this forum by the minds that be. I haven't
    actually tested it myself.
     
    Devin, Jul 7, 2004
    #8
  9. BTO

    BTO Guest

    thx everybody,

    at this time my final result is :

    (setq lst (list 'ATTDIA 0 'FACETRES 0.5 'ATTREQ 1 )) ; I found nothing
    simpler, and it's part of challenge (I forgot to specify it, sorry)

    ; to test, I replaced write-line by princ
    (mapcar '(lambda (x) (if (numberp x) (princ (strcat "\n" (vl-symbol-name
    tmp) " " (rtos x))) (setq tmp x))) lst )

    ; same thing :
    (mapcar
    '(lambda (x)
    (if (numberp x)
    (princ (strcat "\n" (vl-symbol-name tmp) " " (rtos x)))
    (setq tmp x)
    )
    )
    lst
    )

    any advice is welcome

    Bruno Toniutti
     
    BTO, Jul 8, 2004
    #9
  10. BTO

    BTO Guest

    at this time my final result is :
    Hi,
    simpler,

    (setq lst
    (list
    'acadlspasdoc 0
    'apbox 0
    'attdia 0
    'attmode 1
    'attmode 1
    .......
    .....
    )
    )

    ;I removed "strcat" and "setq"
    (mapcar
    '(lambda (x)
    (if (numberp x)
    (write-line (rtos x) file_scr )
    (write-line (vl-symbol-name x) file_scr )
    )
    )
    lst
    )

    and with "if" inside "write-line" :

    (mapcar
    '(lambda (x)
    (write-line (if (numberp x) (rtos x) (vl-symbol-name x)) file_scr )
    )
    lst
    )

    single line :
    (mapcar '(lambda (x) (write-line (if (numberp x) (rtos x) (vl-symbol-name
    x)) file_scr )) lst)


    Bruno Toniutti
     
    BTO, Jul 16, 2004
    #10
  11. BTO

    BTO Guest

    with simplest list :

    (setq lst "acadlspasdoc 0 apbox 0 attdia 0 attmode 1 attmode 1 attreq 1
    ............
    trimmode 1 ucsfollow 0 ucsicon 1 visretain 1 worldview 1")

    (while (> (strlen lst ) 0)
    (setq tmp (read lst ))
    (setq tmp_txt (if (numberp tmp ) (rtos tmp )(vl-symbol-name tmp )))
    (princ (strcat "\n" tmp_txt )) ; I use princ to test result
    (setq lst (substr lst (+ (strlen tmp_txt ) 2)))
    )

    advices are welcome.

    Bruno Toniutti
     
    BTO, Aug 23, 2004
    #11
  12. BTO

    Jürg Menzi Guest

    Hi Bruno

    How about this:
    Code:
    (defun WriteStringToFile (Stg Fil)
    (mapcar
    '(lambda (l)
    (write-line
    (strcat "\n" (if (numberp l) (rtos l) (vl-symbol-name l))))
    Fil
    )
    ) (read (strcat "(" Stg ")"))
    )
    (princ)
    )
    
    Cheers
     
    Jürg Menzi, Aug 24, 2004
    #12
  13. BTO

    Jürg Menzi Guest

    Welcome...¦-)

    I didn't test the code and I've recognized two errors - must be:
    Code:
    (defun WriteStringToFile (Stg Fil)
    (mapcar
    '(lambda (l)
    (write-line
    (if (numberp l) (rtos l) (vl-symbol-name l))
    Fil
    )
    ) (read (strcat "(" Stg ")"))
    )
    (princ)
    )
    
    Cheers
     
    Jürg Menzi, Oct 12, 2004
    #13
  14. BTO

    BTO Guest

    Code:
    [QUOTE]
    (defun WriteStringToFile (Stg Fil)
    (mapcar
    '(lambda (l)
    (write-line
    (if (numberp l) (rtos l) (vl-symbol-name l))
    Fil
    )
    ) (read (strcat "(" Stg ")"))
    )
    (princ)
    )
    
    Cheers[/QUOTE]

    Hi,
    I updated my code and mixed several parts with your line :

    (read (strcat "(" lst ")"))

    and :

    (setq lst "acadlspasdoc 0 apbox 0 attdia 0 attmode 1 attmode 1 attreq 1
    ............
    trimmode 1 ucsfollow 0 ucsicon 1 visretain 1 worldview 1")

    and my post from 07 16 2004 :
    (mapcar
    '(lambda (x)
    (write-line (if (numberp x) (rtos x) (vl-symbol-name x)) file_scr )
    )
    lst
    )

    so i get same result.

    (read (strcat "(" Stg ")")) was the part needed, you found it, and thank you
    again.

    Have a nice day.
    Bruno Toniutti.
     
    BTO, Oct 12, 2004
    #14
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.