(explode) enhancement

Started by Fanda, September 15, 2005, 11:08:34 PM

Previous topic - Next topic

Fanda

Hello!

I would like to do something like this:

"keep it together" => ("keep" "it" "together")



> (explode "keep it together")

("k" "e" "e" "p" " " "i" "t" " " "t" "o" "g" "e" "t" "h" "e" "r")



Is it possible to add one more parameter to 'explode'?

> (explode "keep it together" " ")

("keep" "it" "together")

> (explode "keep,it,together" ",")

("keep" "it" "together")



> (explode "first_last@name.com" "_@.")

("first" "last" "name" "com")



Thank you, Fanda



PS: See 'split' in Tcl.

HPW

#1
What about:



> (parse "keep it together" " ")

("keep" "it" "together")

>
Hans-Peter

Fanda

#2
That will do ;-)



Sorry, I couldn't find it yesterday.



Fanda

Fanda

#3
This is what I did with it:


#
# Split function from Tcl
#
(define (parse-no-empty str c)
  (filter (lambda (x) (not (empty? x))) (parse str c)))

(define (split str-data str-break)
  (if (empty? str-break)
    (explode str-data)
    (begin
      (setq str-data (list str-data))
      (dolist (c (explode str-break))
        (setq str-data (flat (map (lambda (s) (parse-no-empty s c)) str-data)))))))


> (split "Simple sentence,   but useful!" ",! ")

("Simple" "sentence" "but" "useful")



> (split "http://www.yahoo.com">http://www.yahoo.com" ":/.")

("http" "www" "yahoo" "com")



> (split "Hi++++Hello-----Bye!" "+-!")

("Hi" "Hello" "Bye")



Fanda

Fanda

#4
And finally the reason why I thought I would need it:


#
# Word jumble :-)
#

; mix inside of the word   ("hello" -> "hlelo")
(define (mix str)
  (if (> (length str) 3)
    (append (str 0) (join (randomize (explode (1 (- (length str) 2) str)))) (str -1))
    str))

; jumble the words in the sentence
(define (jumble str)
  (let (w "" break ",. -:?!()[]{}+/|=*&^%$#@!~`'<>" result "")
    (dolist (c (explode str))
      (if (find c break)
        (begin
          (write-buffer result (append (mix w) c))
          (setq w ""))
        (write-buffer w c)))
    (write-buffer result (mix w))
    result))


(setq str "According to research at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be in the right place. The rest can be a total mess and you can still read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole.")

(println "n" (jumble str) "n")


=> Aidoccrng to racerseh at Cibamdgre Urtisevniy, it dosen't mtaetr in waht oredr the lettres in a wrod are, the olny imoapnrtt tnhig is taht the frsit and lsat letter be in the rhigt pcale. The rset can be a tatol mses and you can sltil raed it wtihuot a pbolrem. Tihs is bascuee the hmuan mnid deos not raed eevry letetr by isetlf, but the wrod as a wolhe.



Fanda :-)

Lutz

#5
You may also want to look into 'parse' with regular expressions in the break string. See examples in the manual.



Lutz