(explode) enhancement

Q&A's, tips, howto's
Locked
Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

(explode) enhancement

Post by 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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

What about:

> (parse "keep it together" " ")
("keep" "it" "together")
>
Hans-Peter

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

That will do ;-)

Sorry, I couldn't find it yesterday.

Fanda

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

This is what I did with it:

Code: Select all

#
# 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")

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

Fanda

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

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

Code: Select all

#
# 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Lutz

Locked