Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 49:51:11 |
Calls: | 583 |
Files: | 1,138 |
Messages: | 111,303 |
I've got this line:
|2|Dollars|23-dec-1999|Idaho|
And I want to sellect the date (23-dec-1999). If I try with this:
(loop repeat 3 do
(setf Position1 (search "|" line)))
I get Position1=0. I would like to 'jump' the first two '|'
characters, in order to get Position1=10. Afterwards, I could use
'subseq' command, but I am not able.
A couple of points:
a) Make sure you have established a valid lexical binding for
variables you set with setf/setq.
b) Use position instead of search, because you are really just looking
for the position of a single character in a string, not doing a
substring search, which is more expensive.
c) Take a look at the start keyword parameter to position.
That gets you to
(defun get-field-from-line (field line)
"Returns a copy of the given field (0 based index) from the line.
Fields are separated by '|'. If there are fewer fields in line than
field, nil is returned."
(loop for start = 0 then (1+ position)
for position = (and start (position #\| line :start start))
repeat field
when (null position)
do (return nil)
finally
(return (subseq line start position))))