TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: matrix2005in on June 26, 2006, 02:35:53 PM
-
how i can divide combined strings to equal number of strings?/
eg:- the combined string is - ABCDEFGHIJKL
i would like to divide into - ABC DEF GHI JKL
how i can do it??
thanks
mathew
-
Find out how long the stirng is, then find the number of new strings you want. Then use substr.
Hint: strlen, loop with while, substr
-
What do you want the final product to be (a string or a list) and what should the function do when passed strings that don't divide evenly?
-
A string only..but that must be divided equally...the input will be even number and number of strings will be predefined..eg:
input string: GHJLIETYHDWR =12(this will be predefined)
Output must be = GHJL IETY HDWR
thanks
mathew
-
A string only..but that must be divided equally...the input will be even number and number of strings will be predefined..eg:
input string: GHJLIETYHDWR =12(this will be predefined)
Output must be = GHJL IETY HDWR
thanks
mathew
Hi Mathew
(defun str-trim-i (str i)
(if (> (strlen str) 0)
(cons (substr str 1 i) (str-trim-i (substr str (1+ i)) i))
) ;_ if
) ;_ defun
(str-trim-i "GHJLIETYHDWR" 2) ; ->> ("GH" "JL" "IE" "TY" "HD" "WR")
(str-trim-i "GHJLIETYHDWR" 3) ; ->> ("GHJ" "LIE" "TYH" "DWR")
(str-trim-i "GHJLIETYHDWR" 4) ; ->> ("GHJL" "IETY" "HDWR")
(str-trim-i "GHJLIETYHDWR" 5) ; ->> ("GHJLI" "ETYHD" "WR")
(str-trim-i "GHJLIETYHDWR" 6) ; ->> ("GHJLIE" "TYHDWR")
-
Variant with string... :-)
(defun str-trim-i>str (str i)
(if (> (strlen str) i)
(strcat (substr str 1 i) " " (str-trim-i>str (substr str (1+ i)) i))
str
) ;_ if
) ;_ defun
(str-trim-i>str "GHJLIETYHDWR" 2) ;->> "GH JL IE TY HD WR"
(str-trim-i>str "GHJLIETYHDWR" 3) ;->> "GHJ LIE TYH DWR"
(str-trim-i>str "GHJLIETYHDWR" 4) ;->> "GHJL IETY HDWR"
(str-trim-i>str "GHJLIETYHDWR" 5) ;->> "GHJLI ETYHD WR"
(str-trim-i>str "GHJLIETYHDWR" 6) ;->> "GHJLIE TYHDWR"
-
(defun str-trim-i (str i) ...
Very elegant solutions, Elpanov!
-
Very elegant solutions, Elpanov!
Thank! :-)
-
Very nice Evgeniy!
And for a non-recursive version:
(defun string-break (str brk@ / idx result tmp)
(setq idx (- 1 brk@))
(while (/= (setq tmp (substr str (setq idx (+ idx brk@)) brk@)) "")
(if result
(setq result (strcat result " " tmp))
(setq result tmp)
)
)
result
)
(defun string-break2 (str brk@ / idx result tmp)
(setq idx (- 1 brk@))
(while (/= (setq tmp (substr str (setq idx (+ idx brk@)) brk@)) "")
(if result
(setq result (cons tmp result))
(setq result (list tmp))
)
)
(reverse result)
)
-
(defun break_string (txt cnt / string strings)
(setq string (substr txt 1 cnt))
(setq strings (cons string strings))
(while (/= (setq txt (vl-string-left-trim string txt)) "")
(if (/= (setq string (substr txt 1 cnt)) "")
(setq strings (cons string strings))))
(reverse strings))
(break_string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 3)
("ABC" "DEF" "GHI" "JKL" "MNO" "PQR" "STU" "VWX" "YZ")
-
hi thanks that's gr8
another question..regarding looping....code given below is to strip off characters from a set of word..
(defun test ()
(setq afg "GHJ5623JHG56")
(setq drv (dos_strremove (strcat afg) "G"))
(setq drv (dos_strremove (strcat DRV) "H"))
(setq drv (dos_strremove (strcat drv) "J"))
)
actually variable afg wont be predefined like i mentioned here...afg will be include any letters that i dont know so i need to add all alphabet here...instead of repeating same code is there any other way to do this..looping or something??
-
It must be my Sunday morning brain fog ... but I cant determine your requirements.
Do you want the string stripped of ALL alpha characters, upper and lower case ???
... only returning numeric digits ?
.... OR ... ?
-
if ... only returning numeric digits ?
something like this may suit you ...
;;---------------------------------------------------------------------------
(DEFUN numsOnly (str /)
(VL-LIST->STRING (VL-REMOVE-IF-NOT 'numchar-p (VL-STRING->LIST str)))
)
;;---------------------------------------------------------------------------
(DEFUN numchar-p (n) (AND (> n 47) (< n 58)))
;;---------------------------------------------------------------------------
(setq afg "GHJ5623JHG56")
;; ...
(setq stripper (numsOnly afg))
;;=> "562356"
-
Kerry, if it's any consolation, that's what my Saturday afternoon brain thought was wanted as well. :| I was waiting for the task to be clarified.......
-
Hi Jeff,
just checked my news/mail .. looks like Jon on the newsgroup read the same question another way ...
.. and gave a replacement functionality for dos_strremove.
so, yes, just goes to show.
I have always maintained that the question is more important than the answer. .. but perhaps thats my personal problem.
-
It is better to find approximate solution to an exact problem than to find an exact solution to approximate problem.
Author Unknown
-
hi
this is what i want excellent code...thanks verymuch
-
I want excellent code, too, but alas, all I have is me to write it ..... :evil:
mathew, a well placed period "." or comma "," would change the meaning of that sentence quite a bit.... :-)
Kerry, I knew we were right and Jon was missing the mark. :kewl:
CAB, well quoted!
-
this is mine...
(setq t1 "ABCDEFGHIJK")
(setq list2 nil)
(while (/= t1 "")
(setq list1 (substr t1 1 3))
(setq list2 (append list2 (list list1)))
(setq t1 (substr t1 4))
)
(foreach n list2
(if (not text1)(setq text1 " "))
(setq text1 (strcat text1 " " n))
)
(setq text2 (substr text1 3))
text2 = "ABC DEF GHI JK"
list2 = ("ABC" "DEF" "GHI" "JK")
-
hi
...thanks verymuch
Have a play with this too .. though not as easy to understand, perhaps ..
;;---------------------------------------------------------------------------
(DEFUN numsOnly (str /)
(VL-LIST->STRING (VL-REMOVE-IF-NOT '(LAMBDA (n) (AND (> n 47) (< n 58)))
(VL-STRING->LIST str)
)
)
)
;;---------------------------------------------------------------------------
-
(defun test (str)
; Not used VL-...
(if (= str "")
""
(if (< 47 (ascii str) 58)
(strcat
(substr str 1 1)
(test (substr str 2))
) ;_ strcat
(test (substr str 2))
) ;_ if
) ;_ if
) ;_ defun