2013-03-13 11 views
13

Sto provando a prendere l'input dell'utente e memorizzarlo in una lista, ma al posto di una lista composta da una singola stringa, voglio che ogni parola acquisita sia la sua stringa. Esempio:Lisp - Ingresso di divisione in stringhe separate

> (input) 
This is my input. Hopefully this works 

sarebbero tornati:

("this" "is" "my" "input" "hopefully" "this" "works") 

Prendendo atto che io non voglio spazi o segni di punteggiatura nella mia lista finale.

Qualsiasi input sarebbe molto apprezzato.

+0

Cassa http://cl-cookbook.sourceforge.net/strings.html hanno un mazzo di caso di utilizzo comune funziona uno dei quali è uno spazio semplice dividere che è possibile modificare per rimuovere la punteggiatura e simili. –

risposta

17

split-sequence è la soluzione off-the-shelf.

si può anche rotolare il proprio:

(defun my-split (string &key (delimiterp #'delimiterp)) 
    (loop :for beg = (position-if-not delimiterp string) 
    :then (position-if-not delimiterp string :start (1+ end)) 
    :for end = (and beg (position-if delimiterp string :start beg)) 
    :when beg :collect (subseq string beg end) 
    :while end)) 

dove delimiterp verifica se si desidera dividere su questo personaggio, per esempio

(defun delimiterp (c) (or (char= C#\Space) (char= C#\,))) 

o

(defun delimiterp (c) (position c " ,.;/")) 

PS. guardando il valore di rendimento atteso, sembra che vogliate chiamare string-downcase prima del my-split.

PPS. è possibile modificare facilmente my-split per accettare :start, :end, :delimiterp & c.

PPP. Scusa per i bug nelle prime due versioni di my-split. Si prega di considerare che un indicatore che si dovrebbe non rollare la propria versione di questa funzione, ma utilizzare la soluzione off-the-shelf.

+0

Trovo molto materiale su split-sequence, ma a quanto pare ho bisogno di importare il pacchetto cl-utilities, che non riesco proprio a capire =/ #imanewb –

+2

@SeanEvans: attento! 'import' è una funzione CL che * non * vuoi qui! quello che ti serve è * installare * il pacchetto usando, ad esempio, * quicklisp *: '(ql: quickload" split-sequence ")' – sds

+0

Questo ha aiutato molto. Grazie mille. –

1
; in AutoLisp usage (splitStr "get off of my cloud" " ") returns (get off of my cloud) 

(defun splitStr (src delim/word letter) 

    (setq wordlist (list)) 
    (setq cnt 1) 
    (while (<= cnt (strlen src)) 

    (setq word "") 

    (setq letter (substr src cnt 1)) 
    (while (and (/= letter delim) (<= cnt (strlen src))) ; endless loop if hits NUL 
     (setq word (strcat word letter)) 
     (setq cnt (+ cnt 1))  
     (setq letter (substr src cnt 1)) 
    ) ; while 

    (setq cnt (+ cnt 1)) 
    (setq wordlist (append wordlist (list word))) 

) 

    (princ wordlist) 

    (princ) 

) ;defun 
1

C'è cl-ppcre:split:

* (split "\\s+" "foo bar baz 
frob") 
("foo" "bar" "baz" "frob") 

* (split "\\s*" "foo bar baz") 
("f" "o" "o" "b" "a" "r" "b" "a" "z") 

* (split "(\\s+)" "foo bar baz") 
("foo" "bar" "baz") 

* (split "(\\s+)" "foo bar baz" :with-registers-p t) 
("foo" " " "bar" " " "baz") 

* (split "(\\s)(\\s*)" "foo bar baz" :with-registers-p t) 
("foo" " " "" "bar" " " " " "baz") 

* (split "(,)|(;)" "foo,bar;baz" :with-registers-p t) 
("foo" "," NIL "bar" NIL ";" "baz") 

* (split "(,)|(;)" "foo,bar;baz" :with-registers-p t :omit-unmatched-p t) 
("foo" "," "bar" ";" "baz") 

* (split ":" "a:b:c:d:e:f:g::") 
("a" "b" "c" "d" "e" "f" "g") 

* (split ":" "a:b:c:d:e:f:g::" :limit 1) 
("a:b:c:d:e:f:g::") 

* (split ":" "a:b:c:d:e:f:g::" :limit 2) 
("a" "b:c:d:e:f:g::") 

* (split ":" "a:b:c:d:e:f:g::" :limit 3) 
("a" "b" "c:d:e:f:g::") 

* (split ":" "a:b:c:d:e:f:g::" :limit 1000) 
("a" "b" "c" "d" "e" "f" "g" "" "") 

http://weitz.de/cl-ppcre/#split

Per i casi più comuni c'è la (nuova, "moderna e coerente") libreria di manipolazione cl-str stringa:

(str:words "a sentence with spaces") ; cut with spaces, returns words 
(str:replace-all "," "sentence") ; to easily replace characters, and not treat them as regexps (cl-ppcr treats them as regexps) 

È avere cl-slug da rimuovere ve caratteri non ASCII e anche la punteggiatura:

(asciify "Eu André!") ; => "Eu Andre!" 
0
(defun splitStr (src pat /) 
    (setq wordlist (list)) 
    (setq len (strlen pat)) 
    (setq cnt 0) 
    (setq letter cnt) 
    (while (setq cnt (vl-string-search pat src letter)) 
     (setq word (substr src (1+ letter) (- cnt letter))) 
     (setq letter (+ cnt len)) 
     (setq wordlist (append wordlist (list word))) 
    ) 
    (setq wordlist (append wordlist (list (substr src (1+ letter))))) 
) 
+0

Sebbene questo possa rispondere alla domanda, è sempre opportuno fornire una spiegazione del proprio codice e dei riferimenti che potrebbero essere utili. Controlla [risposta] per i dettagli sulla risposta alle domande. –

Problemi correlati