Analyze a text, is a number or a character ?

Discussion in 'AutoCAD' started by BTO, Feb 25, 2005.

  1. BTO

    BTO Guest

    hi,

    (setq Txt ("Level-1-building-A"))

    i want to replace chars "-" by "_"
    until today I used :

    (vl-string-translate " -" "__" Txt )

    but now i don't want to replace "-" by "_" if next character is a number.
    I want to get this result :

    "Level-1_building_A"

    how to determine if next character is a number ?
    in this case I need to analyse "1"

    Any idea is welcome

    Bruno Toniutti
    (sorry for my english level)
     
    BTO, Feb 25, 2005
    #1
  2. BTO

    BTO Guest

    I had a coffee...and I found.

    I need simply to compare this character with chars : 0 1 2 3 4 5 6 7 8 9

    have a good day

    Bruno Toniutti
     
    BTO, Feb 25, 2005
    #2
  3. BTO

    BTO Guest

    i wanted to said :

    (setq Txt "Level-1-building-A")

    Bruno Toniutti
     
    BTO, Feb 25, 2005
    #3
  4. BTO

    Joe Burke Guest

    Bruno,

    Or maybe like this.

    Command: (numberp (read "A"))
    nil

    Command: (numberp (read "2"))
    T

    Joe Burke
     
    Joe Burke, Feb 25, 2005
    #4
  5. BTO

    petervose Guest

    Try this
    (defun xreplace (txt / t2 update k)
    (setq update nil)
    (setq t2 (vl-string-translate "-" "_" txt))
    (setq k 1)
    (repeat (- (strlen t2) 1)
    (if (and (isi (substr t2 k 1)) (= (substr t2 (- k 1) 1) "_"))
    (progn
    (setq t2 (strcat (substr t2 1 (- k 2)) "-" (substr t2 k)))
    (setq update T)
    )
    )
    (setq k (+ 1 k))
    )
    (if update t2 txt)
    )



    (defun isi (tval / res b)
    (setq res nil)
    (if (= (type tval) 'STR)
    (progn
    (setq res T)
    (setq b 1)
    (repeat (strlen tval)
    (if (or (< (ascii (substr tval b 1)) 48) (> (ascii (substr tval b 1)) 57))
    (setq res nil)
    )
    (setq b (+ 1 b))
    )
    )
    )
    (if (= tval "") (setq res nil))
    (setq res res)
    )
     
    petervose, Feb 25, 2005
    #5
  6. BTO

    BTO Guest

    Joe,
    your

    (numberp (read NextChar))

    is more elegant than my :

    (vl-string-search NextChar "0123456789")

    thank you

    Bruno Toniutti
     
    BTO, Feb 25, 2005
    #6
  7. BTO

    BTO Guest

    Petervose,

    works fine,
    thank you


    finaly i use :

    to test :
    (setq Txt "-1abcdef-123-ABCD_456_798-")

    ;
    Code:
    ; 1.050225 Bruno Toniutti
    (setq NewTxt "")
    (while (setq Position (vl-string-search "-" Txt))
    (if (/= Position (- (strlen Txt) 1))
    (progn
    (setq Part1 (substr Txt 1 Position ))
    (setq NextChar (substr Txt (+ Position 2) 1 ))
    ;(if (vl-string-search NextChar "0123456789") (setq NewChar
    "-") (setq NewChar "_"))
    (if (numberp (read NextChar)) (setq NewChar "-") (setq
    NewChar "_")) ; Joe Burke
    (setq NewTxt (strcat NewTxt Part1 NewChar))
    (setq Txt (substr Txt (+ Position 2)))
    )
    (setq Txt (vl-string-translate "-" "_" Txt))
    )
    )
    (setq Txt (strcat NewTxt Txt))
    ;[code]
    
    
    Bruno Toniutti
     
    BTO, Feb 25, 2005
    #7
  8. It probably doesn't matter much, but here's
    yet another way:

    (defun my-string-translate (instr)
    (vl-list->string
    (my-string-translate-aux
    (vl-string->list instr)
    )
    )
    )

    (defun my-string-translate-aux (lst)
    (cond
    ( (not lst) nil)
    ( (or (/= (car lst) 45)
    (> 58 (cadr lst) 47))
    (cons (car lst)
    (my-string-translate-aux (cdr lst))))
    (t (cons 95 (my-string-translate-aux (cdr lst))))
    )
    )
     
    Tony Tanzillo, Feb 25, 2005
    #8
  9. BTO

    T.Willey Guest

    FYI

    (numberp (read ".")) Will error.

    Tim
     
    T.Willey, Feb 25, 2005
    #9
  10. Tim pointed out one of several examples of why, so
    I'll reiterate what I've said in the past, which is that
    relying on the (read) function to do things that it was
    not really intended to do, is problematic at best.

    (read) is for reading LISP expressions, and it doesn't
    afford one the luxury of knowing exactly what causes
    reader errors like 'malformed list', or 'misplaced dot', and
    so on.

    Don't use it for things it wasn't designed to do, if there
    is a safer way to do it. If you are going to use it for
    things like this and you make no assumptions about what
    is in the string, then at least, the call must be guarded
    by a vl-catch-all-apply.
     
    Tony Tanzillo, Feb 26, 2005
    #10
  11. BTO

    Joe Burke Guest

    Tony,

    It matters to me.

    I'm kicking myself for forgetting your caution against using read in inappropriate
    fashions. That should have stuck with me.

    Joe Burke

     
    Joe Burke, Feb 27, 2005
    #11
  12. BTO

    BTO Guest

    thanks T.Willey, Tony, Joe and Petervose

    Txt is a file name, therefore it could happen that "." is used.

    Even if I understand principle, Tony's solution is very difficult to
    entirely understands...

    finaly i use :

    ;to test :
    (setq Txt "--4cxvcAë-1abcde..f-123-ABCD_456_798-")

    ; 1.050228 Bruno Toniutti
    (setq NewTxt "")
    (while (setq Position (vl-string-search "-" Txt))
    (if (/= Position (- (strlen Txt) 1))
    (progn
    (setq NextChar (substr Txt (+ Position 2) 1 ))
    (setq NewTxt
    (strcat
    NewTxt
    (substr Txt 1 Position )
    (if (vl-string-search NextChar "0123456789") "-" "_")
    )
    )
    (setq Txt (substr Txt (+ Position 2)))
    )
    (setq Txt (vl-string-translate "-" "_" Txt))
    )
    )
    (setq Txt (strcat NewTxt Txt))


    But with some of Tony's ideas, I explore another way :

    (setq Txt "-4cxvcAë-1abcde..f-123-ABCD_456_798-")
    (setq ListTxt (vl-string->list Txt))
    (setq i 1)
    (setq ListReturn '())
    (foreach Char ListTxt
    (setq ListReturn
    (append ListReturn
    (if (and (= Char 45)(not (> 58 (nth i ListTxt) 47)))
    '(95)
    (list Char)
    )
    )
    )
    (setq i (1+ i))
    )
    (setq Txt (vl-list->string ListReturn))


    Bruno Toniutti
     
    BTO, Feb 28, 2005
    #12
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.