I've only stratched the surface of lisp, but I'm trying. I want to
convert a string to a list of strings. For example,
"Now is the time" ==> ("Now" "is" "the" "time")
The best I've been able to do with the functions read-from-string and
concatenate is this
"Now is the time" ==> (Now is the time)
With the list of strings, I can do an interated process to call
another program. I don't know what to do with a list of symbols.
Any help is appreciated.
I.B.
····@news2.stic.net (IBMackey) writes:
> I've only stratched the surface of lisp, but I'm trying. I want to
> convert a string to a list of strings. For example,
>
> "Now is the time" ==> ("Now" "is" "the" "time")
>
> The best I've been able to do with the functions read-from-string and
> concatenate is this
>
> "Now is the time" ==> (Now is the time)
>
> With the list of strings, I can do an interated process to call
> another program. I don't know what to do with a list of symbols.
you can do
(mapcar #'symbol-name '(Now is the time))
to make a list of strings
("NOW" "IS" "THE" "TIME")
however, i suspect that parsing the string without converting to
symbols will be a better solution. it avoids the reader case
conversion stuff.
--
J o h a n K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!
From: David Hanley
Subject: Re: Convert string of words to list of strings
Date:
Message-ID: <377A5C3F.2898AFF0@ncgr.o_r_g>
Johan Kullstam wrote:
> you can do
>
> (mapcar #'symbol-name '(Now is the time))
>
> to make a list of strings
>
> ("NOW" "IS" "THE" "TIME")
>
> however, i suspect that parsing the string without converting to
> symbols will be a better solution. it avoids the reader case
> conversion stuff.
That's a nifty way to do it, but it also may create a lot of symbols, too.
Here's how I did it:
(defun mgs()
(make-array 10 :fill-pointer 0 :element-type 'character))
(defun parse-str( str )
(let ((res (list (mgs))))
(map nil #'(lambda(x)(if (char= x #\space) (push (mgs) res)
(vector-push-extend x (car res)))) str)
(delete-if #'(lambda(x)(string= x "")) (nreverse res))))
I think it's a fair tradeoff between efficency and compactness.
dave
From: David Bakhash
Subject: Re: Convert string of words to list of strings
Date:
Message-ID: <cxjzp1i36ae.fsf@acs5.bu.edu>
hey,
here's the function. It's been around the block a few times. It does the
job, and I use it all the time.
(defun split (string &optional max (ws #(#\space #\tab #\newline #\return)))
"Split `string' along whitespace as defined by the sequence `ws'.
The whitespace is elided from the result. The whole string will be
split, unless `max' is a non-negative integer, in which case the
string will be split into `max' tokens at most, the last one
containing the whole rest of the given `string', if any."
(declare (type string string)
(type (or null fixnum) max)
(type sequence ws))
(flet ((is-ws (char)
(declare (type character char))
(find char ws)))
(loop
for start = (position-if-not #'is-ws string)
then (position-if-not #'is-ws string :start index)
for index = (and start
(unless (and max (= (1+ word-count) max))
(position-if #'is-ws string :start start)))
while start
collect (subseq string start index)
count 1 into word-count
while index)))
dave
On 29 Jun 1999 20:22:21 GMT, IBMackey <····@news2.stic.net> wrote:
>I've only stratched the surface of lisp, but I'm trying. I want to
>convert a string to a list of strings. For example,
>
>"Now is the time" ==> ("Now" "is" "the" "time")
>
>The best I've been able to do with the functions read-from-string and
>concatenate is this
>
>"Now is the time" ==> (Now is the time)
>
>With the list of strings, I can do an interated process to call
>another program. I don't know what to do with a list of symbols.
The following is usable in Scheme to do this:
(define (next-somechar string startpos endpos somechar)
(let loop
((pos startpos))
(cond
((>= pos endpos) endpos) ; Exit- reached end of string
((char=? (string-ref string pos) somechar) pos) ; Reached "somechar"
(else
(loop (+ pos 1))))))
(define (split-on-somechar sourcestring somechar)
(let loop
((pos 0)
(endpos (string-length sourcestring))
(result '()))
(cond
((>= pos endpos) result)
(else
(let ((nextwhatever
(next-somechar sourcestring pos endpos somechar)))
(loop
(+ nextwhatever 1)
endpos
(append result (list (substring sourcestring pos
nextwhatever)))))))))
(split-on-somechar "Now is the time" \#space) would provide the desired
result.
This uses the "named let" form, which provides a fairly efficient
looping construct. Presumably a similar approach, albeit with different
names, would work in CL.
--
"Linux: the operating system with a CLUE... Command Line User
Environment". (seen in a posting in comp.software.testing)
········@hex.net- <http://www.hex.net/~cbbrowne/langlisp.html>
····@news2.stic.net (IBMackey) writes:
> I've only stratched the surface of lisp, but I'm trying. I want to
> convert a string to a list of strings. For example,
>
> "Now is the time" ==> ("Now" "is" "the" "time")
>
> The best I've been able to do with the functions read-from-string and
> concatenate is this
>
> "Now is the time" ==> (Now is the time)
>
> With the list of strings, I can do an interated process to call
> another program. I don't know what to do with a list of symbols.
>
> Any help is appreciated.
>
> I.B.
You can use the following function, which is also an excellent example
of how non-trivial loops can be pretty confusing, and thus should be
avoided:
(defun split (string &optional max (ws '(#\Space #\Tab)))
(flet ((is-ws (char) (find char ws)))
(loop for start = (position-if-not #'is-ws string)
then (position-if-not #'is-ws string :start index)
for index = (and start
(if (and max (= (1+ word-count) max))
nil
(position-if #'is-ws string :start start)))
while start
collect (subseq string start index)
count 1 into word-count
while index)))
This function somewhat mirrors Perl's split, and could be simplified a
bit like this (non-tested), if there is no optional maximum cut-off:
(defun split (string &optional (ws '(#\Space #\Tab)))
(flet ((is-ws (char) (find char ws)))
(loop for start = (position-if-not #'is-ws string)
then (position-if-not #'is-ws string :start index)
for index = (and start
(position-if #'is-ws string :start start))
while start
collect (subseq string start index)
while index)))
Regs, Pierre.
--
Pierre Mai <····@acm.org> PGP and GPG keys at your nearest Keyserver
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Christopher R. Barry
Subject: Re: Convert string of words to list of strings
Date:
Message-ID: <871zeu5swb.fsf@2xtreme.net>
····@acm.org (Pierre R. Mai) writes:
> This function somewhat mirrors Perl's split, and could be simplified a
> bit like this (non-tested), if there is no optional maximum cut-off:
>
> (defun split (string &optional (ws '(#\Space #\Tab)))
> (flet ((is-ws (char) (find char ws)))
> (loop for start = (position-if-not #'is-ws string)
> then (position-if-not #'is-ws string :start index)
> for index = (and start
> (position-if #'is-ws string :start start))
> while start
> collect (subseq string start index)
> while index)))
Just nit-picking.
(defun split (string &optional (ws '(#\Space #\Tab)))
(flet ((is-ws (char) (find char ws)))
(loop for start = (position-if-not #'is-ws string)
then (position-if-not #'is-ws string :start index)
for index = (position-if #'is-ws string :start start)
collect (subseq string start index)
while index)))
Christopher
······@2xtreme.net (Christopher R. Barry) writes:
> ····@acm.org (Pierre R. Mai) writes:
>
> > This function somewhat mirrors Perl's split, and could be simplified a
> > bit like this (non-tested), if there is no optional maximum cut-off:
> >
> > (defun split (string &optional (ws '(#\Space #\Tab)))
> > (flet ((is-ws (char) (find char ws)))
> > (loop for start = (position-if-not #'is-ws string)
> > then (position-if-not #'is-ws string :start index)
> > for index = (and start
> > (position-if #'is-ws string :start start))
> > while start
> > collect (subseq string start index)
> > while index)))
>
> Just nit-picking.
>
> (defun split (string &optional (ws '(#\Space #\Tab)))
> (flet ((is-ws (char) (find char ws)))
> (loop for start = (position-if-not #'is-ws string)
> then (position-if-not #'is-ws string :start index)
> for index = (position-if #'is-ws string :start start)
> collect (subseq string start index)
> while index)))
No, this doesn't work, since we can have only whitespace at the end of
a string (which is to be ignored), which would result in start
becoming nil, which is not a valid index designator for POSITION-IF's
:start argument:
* (split "a b ccc ")
Type-error in KERNEL::OBJECT-NOT-FIXNUM-ERROR-HANDLER:
NIL is not of type FIXNUM
Restarts:
0: [ABORT] Return to Top-Level.
Debug (type H for help)
("DEFUN POSITION-IF" #<Closure Over Function IS-WS {4809CA41}>
"a b ccc " 268434278 2)[:OPTIONAL]
Source:
; File: target:code/seq.lisp
; File has been modified since compilation:
; target:code/seq.lisp
; Using form offset instead of character position.
(DEFUN POSITION-IF (TEST SEQUENCE &KEY FROM-END (START 0) ...)
"Returns the zero-origin index of the first element satisfying test(el)"
(DECLARE (FIXNUM START))
...)
0]
So you either have to trim whitespace of the end of the string first
(with the resulting performance penalty), or you have to write it in
the convoluted way I did. Or probably better: write a simple
state-machine to do this, which is probably faster, easier to
understand and better specified.
Regs, Pierre.
--
Pierre Mai <····@acm.org> PGP and GPG keys at your nearest Keyserver
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Christopher R. Barry
Subject: NSPLIT and SIMPLE-STRING structure reuse. (was Re: Convert string of words to list of strings)
Date:
Message-ID: <87yah148bh.fsf_-_@2xtreme.net>
Is there a way to reuse SIMPLE-STRING structure that is more efficient
than not reusing the structure by calling SUBSEQ? I wasted a fair
ammount of time trying to write a low-garbage, reuse version of SPLIT
called NSPLIT (should be garbage-free if you don't cons the result
list but use VALUES) except the only way I can figure out to get a
"slice" of a SIMPLE-STRING is to use MAKE-ARRAY with :DISPLACED-TO and
:DISPLACED-INDEX-OFFSET which seems to be more expensive garbage,
space and time-wise then just calling SUBSEQ even though it is
essentially creating a pointer to the old structure.
Pointers, anyone? (Pun alert.)
Christopher
From: David Bakhash
Subject: Re: NSPLIT and SIMPLE-STRING structure reuse. (was Re: Convert string of words to list of strings)
Date:
Message-ID: <cxjso782zh6.fsf@acs5.bu.edu>
in that format, I doubt it'll help anyone. It's probably better to stick to
uuencoding if you're gonna send stuff over in non-plaintext format. I think
you're using Gnus, but somehow I wasn't able to decode it, and I use Gnus too.
dave
From: Christopher R. Barry
Subject: Re: NSPLIT and SIMPLE-STRING structure reuse. (was Re: Convert string of words to list of strings)
Date:
Message-ID: <87u2ro430a.fsf@2xtreme.net>
David Bakhash <·····@bu.edu> writes:
> in that format, I doubt it'll help anyone. It's probably better to stick to
> uuencoding if you're gonna send stuff over in non-plaintext format. I think
> you're using Gnus, but somehow I wasn't able to decode it, and I use Gnus too.
That was in plaintext. The trn newsreader displays it fine to. You may
be having a MULE problem.
Christopher
From: Pekka P. Pirinen
Subject: Re: NSPLIT and SIMPLE-STRING structure reuse. (was Re: Convert string of words to list of strings)
Date:
Message-ID: <ixg134jw9g.fsf@gaspode.cam.harlequin.co.uk>
······@2xtreme.net (Christopher R. Barry) writes:
> Is there a way to reuse SIMPLE-STRING structure that is more efficient
> than not reusing the structure by calling SUBSEQ? I wasted a fair
> ammount of time trying to write a low-garbage, reuse version of SPLIT
> called NSPLIT
Don't be afraid to produce garbage: Recycling short-lived objects is
very efficient. Tuning like this is better left to the stage where
you can see if it makes sense in the context of your application.
> a "slice" of a SIMPLE-STRING is to use MAKE-ARRAY with :DISPLACED-TO
> and :DISPLACED-INDEX-OFFSET which seems to be more expensive
> garbage, space and time-wise then just calling SUBSEQ even though it
> is essentially creating a pointer to the old structure.
That MAKE-ARRAY will have to allocate some kind of an array header to
hold the dimensions and the pointer to the displace target. This will
probably be larger than a typical English word, perhaps even larger
than a SIMPLE-STRING containing a typical English word.
If it really matters, you need to change your code so that words are
passed around as three values: string, start, end.
--
Pekka P. Pirinen
Harlequin Group plc (but not for long)
Technology isn't just putting in the fastest processor and most RAM -- that's
packaging. - Steve Wozniak
Since I hate LOOP I had to write my own tokenizers.
I only want to add that all these tokenizers omit null tokens, but
SDF-like or CDF-like formats may want to keep the null tokens, ie. the
number of spaces or commas can be important. I called that version
string-split.
(strtok ",,1,2" ",") => ("1" "2")
(string-split ",,1,2" ",") => ("" "" "1" "2")
;;; Converts string with delimiters into string list.
;;; Ignore repeated delims such as white space.
;;; The order of chars in delim is not important.
;;; Also named lex-string in some Common Lisps.
;;; (strtok " 2 3 " " ") => ("2" "3")
;;; (strtok "f 1,3" ", ") => ("f" "1" "3")
;;; So far only this bad version of mine, unoptimized.
;;; Also see lex-string in Corman Lisp examples\utility-functions.lisp
;;; I use strings as delims (for AutoLISP compatibility),
;;; otherwise it would be simplier.
(defun strtok (s delims)
"List of tokenized string parts"
(let ((len (length s))
(delims (string->list (string delims)))
(s1 "")
(lst))
(do ((c)(i (1- len) (decf i)))
((< i 0))
(setq c (aref s i))
(if (member c delims)
(if (string/= s1 "") ; no null tokens
(setq lst (cons s1 lst)
s1 ""))
(setq s1 (strcat (string c) s1)))
)
(if (string/= s1 "")
(cons s1 lst) ; no ("" "1" "2")!
lst)))
;;; (string->list "1234") => (#\1 #\2 #\3 #\4)
(defun string->list (s)
"List of chars from string"
(do ((lst)
(i (1- (length (the string s))) (1- i)))
((minusp i) lst)
(push (aref s i) lst)))
or this version taken from Corman Lisp, again without LOOP
;;;; from Larry Hunter
(defun lex-string (string &optional
(whitespace-chars '(#\space #\newline)))
"Separates a string at whitespace and returns a list of strings"
(flet ((whitespace-char? (char)
(member char whitespace-chars :test #'char=)))
(let ((tokens nil))
(do* ((token-start (position-if-not #'whitespace-char? string)
(when token-end
(position-if-not #'whitespace-char? string
:start (1+ token-end))))
(token-end (position-if #'whitespace-char? string
:start token-start)
(when token-start
(position-if #'whitespace-char? string
:start token-start))))
((null token-start) (nreverse tokens))
(push (subseq string token-start token-end) tokens)))))
····@acm.org (Pierre R. Mai) wrote:
>····@news2.stic.net (IBMackey) writes:
>
>> I've only stratched the surface of lisp, but I'm trying. I want to
>> convert a string to a list of strings. For example,
>>
>> "Now is the time" ==> ("Now" "is" "the" "time")
>>
>> The best I've been able to do with the functions read-from-string and
>> concatenate is this
>>
>> "Now is the time" ==> (Now is the time)
>>
>> With the list of strings, I can do an interated process to call
>> another program. I don't know what to do with a list of symbols.
>>
>> Any help is appreciated.
>>
>> I.B.
>
>You can use the following function, which is also an excellent example
>of how non-trivial loops can be pretty confusing, and thus should be
>avoided:
>
>(defun split (string &optional max (ws '(#\Space #\Tab)))
> (flet ((is-ws (char) (find char ws)))
> (loop for start = (position-if-not #'is-ws string)
> then (position-if-not #'is-ws string :start index)
> for index = (and start
> (if (and max (= (1+ word-count) max))
> nil
> (position-if #'is-ws string :start start)))
> while start
> collect (subseq string start index)
> count 1 into word-count
> while index)))
>
>This function somewhat mirrors Perl's split, and could be simplified a
>bit like this (non-tested), if there is no optional maximum cut-off:
>
>(defun split (string &optional (ws '(#\Space #\Tab)))
> (flet ((is-ws (char) (find char ws)))
> (loop for start = (position-if-not #'is-ws string)
> then (position-if-not #'is-ws string :start index)
> for index = (and start
> (position-if #'is-ws string :start start))
> while start
> collect (subseq string start index)
> while index)))
--
Reini Urban
http://xarch.tu-graz.ac.at/autocad/news/faq/autolisp.html
My name appeared in some lexing code quoted above, but unfortunately the
code appears to have been mangled in the quotation. Here's the correct
version:
(defun lex-string (string &key (whitespace '(#\space #\newline)))
"Separates a string at whitespace and returns a list of strings"
(flet ((whitespace? (char) (member char whitespace :test #'char=)))
(let ((tokens nil))
(do* ((token-start
(position-if-not #'whitespace? string)
(when token-end
(position-if-not #'whitespace? string :start (1+ token-end))))
(token-end
(when token-start
(position-if #'whitespace? string :start token-start))
(when token-start
(position-if #'whitespace? string :start token-start))))
((null token-start) (nreverse tokens))
(push (subseq string token-start token-end) tokens)))))
Larry
--
Lawrence Hunter, Ph.D. Chief, Molecular Statistics and Bioinformatics
National Cancer Institute email: ·······@nih.gov
Federal Building, Room 3C06 phone: +1 (301) 402-0389
Bethesda, MD 20892 USA fax: +1 (301) 480-0223
Here's a semi-multi-everything string->list function:
(defun string->list (string &key (start 0) char-bag delimiter-test
(post-process #'subseq))
"Converts STRING into a list,
starting at START;
dividing words at boundaries defined by characters in CHAR-BAG,
or at boundaries defined by DELIMITER-TEST,
(default boundaries: space or non-graphic characters)
each item is run through POST-PROCESS (defaults to SUBSEQ) as it is
created,
having the same signature as SUBSEQ. It should NOT be destructive."
(let ((test (cond
(delimiter-test)
(char-bag #'(lambda (ch) (member ch char-bag :test #'char=)))
(t #'(lambda (ch) (or (char= ch #\Space) (not
(graphic-char-p ch))))))))
(labels ((string->list-helper (string start results)
(let ((start-of-item (position-if (complement test) string
:start start)))
(if start-of-item
(let ((end-of-item (position-if test string :start
start-of-item)))
(if end-of-item
(cons (funcall post-process string start-of-item
end-of-item)
(string->list-helper string end-of-item
results))
(list (funcall post-process string start-of-item))))
'()))))
(string->list-helper string start '()))))
The default behavior is what you want, I think.
Here's a user of string->list that converts to symbols.
(defun string->symbols (string &key (to-upper-p NIL) (additional-chars
'(#\-)) (package *package*))
"Converts a string into a list of symbols interned into PACKAGE, ignoring
everything but alphanumerics and characters in ADDITIONAL-CHARS. If
TO-UPPER-P is true,
interns as upper case."
(string->list string
:post-process #'(lambda (str start &optional end)
(let ((str (subseq str start end)))
(intern (if to-upper-p (nstring-upcase str)
str) package)))
:delimiter-test #'(lambda (ch) (not (or (alphanumericp ch)
(member ch
additional-chars :test #'char=))))))
IBMackey wrote in message <··········@nnrp4.farm.idt.net>...
>I've only stratched the surface of lisp, but I'm trying. I want to
>convert a string to a list of strings. For example,
>
>"Now is the time" ==> ("Now" "is" "the" "time")
>
>The best I've been able to do with the functions read-from-string and
>concatenate is this
>
>"Now is the time" ==> (Now is the time)
>
>With the list of strings, I can do an interated process to call
>another program. I don't know what to do with a list of symbols.
>
>Any help is appreciated.
>
>I.B.
Use PerlCOM or some other bus to Perl. Here's your example using
PerlCOM.
Why write a state machine and all that other stuff? Yeah portable, but
the bus...
First, the code:
(let ((perltool (ole:ask-for-autotool
"PerlCOM.Script"
ole:CLSCTX_INPROC_SERVER)))
(ole:auto-method perltool
"EvalScript"
"$greeting='(\"'.join('\" \"', split(/ /, qq[Now is the time for
PerlCOM])).'\")';")
(format t "... and then PerlCOM said: ~A"
(ole:auto-getf perltool :greeting))
Here's the session:
>
; Loading D:\acl50lite\ole\samples\activePerl\perl.cl
> (test)
Starting OLE PerlCOM test.
... and then PerlCOM said: ("Now" "is" "the" "time" "for" "PerlCOM")
The PerlCOM test is complete.
>
Now, Lisp is so great, it ought to be able to coax that property
into an actual list, right?
-----Original Message-----
From: ····@news2.stic.net (IBMackey) [···········@news2.stic.net]
Posted At: Tuesday, June 29, 1999 4:22 PM
Posted To: lisp
Conversation: Convert string of words to list of strings
Subject: Convert string of words to list of strings
I've only stratched the surface of lisp, but I'm trying. I want to
convert a string to a list of strings. For example,
"Now is the time" ==> ("Now" "is" "the" "time")
The best I've been able to do with the functions read-from-string and
concatenate is this
"Now is the time" ==> (Now is the time)
With the list of strings, I can do an interated process to call
another program. I don't know what to do with a list of symbols.
Any help is appreciated.
I.B.
····@news2.stic.net (IBMackey) writes:
> "Now is the time" ==> ("Now" "is" "the" "time")
As an exercise in sillyness I once wrote a version using REDUCE...
(defun explode-string (string)
(let ((almost
(delete nil
(reduce
#'(lambda (item lol)
(if (char= #\space item)
(cons '() lol)
(cons (cons item (first lol)) (rest lol))))
string :from-end t :initial-value '(())))))
(map-into almost (lambda (list) (coerce list 'string)) almost)))
Note that this version will treat multiple spaces as a single
separator and ignore leading and trailing spaces.
Stig Hemmer,
Jack of a Few Trades.
PS: This was some time ago. I _think_ I proved that MAP-INTO would
never change quoted structure, but I don't recall that proof now.
I'm much too tired right now to really think this through.
····@news2.stic.net (IBMackey) posted on 29 June 1999:
> I've only stratched the surface of lisp, but I'm trying. I want to
> convert a string to a list of strings. For example,
>
> "Now is the time" ==> ("Now" "is" "the" "time")
>
> The best I've been able to do with the functions read-from-string and
> concatenate is this
>
> "Now is the time" ==> (Now is the time)
>
> With the list of strings, I can do an interated process to call
> another program. I don't know what to do with a list of symbols.
>
> Any help is appreciated.
>
> I.B.
I use the beautiful Scheme interpreter SCM, with the standard Scheme
library SLIB. The main author of SCM and SLIB is Aubrey Jaffer, whose
web site is:
http://www-swiss.ai.mit.edu/~jaffer/index.html
I append the transcript of a session in which I first require 'regex, so
that I can use the regular expression library, then define blank+ to be
the result of compiling the regular expression:
\ (\ )*
Then I define the procedure split-at-blank+, which as you can see just
takes the result of the regex library function string-split and turns the
result, which is a vector, into a list, using the standard function
vector->list. Then I define s to be a certain string with blanks. Then I
require 'pretty-print, so that I have the pretty-print library functions
available. The rest should be clear.
onr
> (require 'regex)
;Evaluation took 10 mSec (0 in gc) 1246 cells work, 256 env, 5129 bytes other
#<unspecified>
> (define blank+ (regcomp "\ (\ )*"))
;Evaluation took 0 mSec (0 in gc) 14 cells work, 0 env, 476 bytes other
#<unspecified>
> (define split-at-blank+
(lambda (s)
(vector->list (string-split blank+ s))))
;Evaluation took 0 mSec (0 in gc) 22 cells work, 0 env, 47 bytes other
#<unspecified>
> (define s "This is a string with blanks. And even two blanks in a row. And more. Oi. ")
;Evaluation took 0 mSec (0 in gc) 8 cells work, 0 env, 199 bytes other
#<unspecified>
> (require 'pretty-print)
;loading /usr/lib/slib/pp
; loading /usr/lib/slib/genwrite
; done loading /usr/lib/slib/genwrite.scm
;done loading /usr/lib/slib/pp.scm
;Evaluation took 20 mSec (0 in gc) 1846 cells work, 120 env, 1141 bytes other
#<unspecified>
> (define s-split-at-blanks (split-at-blank+ s))
;Evaluation took 10 mSec (0 in gc) 178 cells work, 3 env, 749 bytes other
#<unspecified>
> (pretty-print s-split-at-blanks)
("This"
"is"
"a"
"string"
"with"
"blanks."
"And"
"even"
"two"
"blanks"
"in"
"a"
"row."
"And"
"more."
"Oi.")
;Evaluation took 0 mSec (0 in gc) 974 cells work, 4648 env, 202 bytes other
8
> (quit)
Don't quibble over milliseconds. Get it to work, then worry about
resource efficiency. This news group is a good place to ask questions.
I've learned more here than anywhere else.
This will take a string and a single token (defaulting to the space
char), parsing the string by the token, and removing null or empty
strings. The implementation may be a few milliseconds slower than
another method, but it is pretty rare when such things really matter,
and the use of more "common" lisp makes it a little more clear when you
aren't as knowledgeable as some of the gents on this ng.
(defun string-parse (str &key (token #\space))
(do* ((p (position token str)
(position token str))
(strings (list (if p (subseq str 0 p) str))
(cons (if p (subseq str 0 p) str) strings)))
((not p) (remove-if
#'(lambda(obj)(string= obj ""))
(nreverse strings)))
(setf str (subseq str (1+ p) (length str)))))
Syntax...
(string-parse "comp.lang.lisp" :token #\.) => ("comp" "lang" "lisp")
sl