I'm trying to read a list by using LISP. My list is a file that
resembles this:
----------//-------
1,John,Engineer
2,Peter,Director
3,Caroll,Engineer
...
-------------//-----
I read on the Internet that this kind of file is called a "comma
separated list". Could anybody help me to read these files?
I was searching the web, but I did not find any tip...
Thank you very much
> Hi!
>
> I'm trying to read a list by using LISP.
How nice that you're trying. Could you also please tell us *how* are
you doing this?
> My list is a file that resembles this:
>
> ----------//-------
> 1,John,Engineer
> 2,Peter,Director
> 3,Caroll,Engineer
> ...
> -------------//-----
>
> I read on the Internet that this kind of file is called a "comma
> separated list". Could anybody help me to read these files?
Well -- do *you* really need help in reading the file?
> I was searching the web, but I did not find any tip...
Oh how nice you told about what else you're doing.
> Thank you very much
Thank you for telling us about your deeds. And you know what -- you
are not alone! You are already a third person to ask this kind of
question recently. Try searching the web more carefully. I suggest
starting at http://groups.google.com.
--
Janis Dzerins
Eat shit -- billions of flies can't be wrong.
> Hi!
>
> I'm trying to read a list by using LISP. My list is a file that
> resembles this:
>
> ----------//-------
> 1,John,Engineer
> 2,Peter,Director
> 3,Caroll,Engineer
> ...
> -------------//-----
>
> I read on the Internet that this kind of file is called a "comma
> separated list". Could anybody help me to read these files?
(defun almost-doing-your-homework (the-file)
(with-open-file (f the-file :direction :input)
(loop for line = (read-line f nil nil)
while line
do (print line))))
Cheers
--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488
719 Broadway 12th Floor fax +1 - 212 - 995 4122
New York, NY 10003, USA http://bioinformatics.cat.nyu.edu
"Hello New York! We'll do what we can!"
Bill Murray in `Ghostbusters'.
Shure. Here's how:
1 learn some Lisp (start at www.lisp.org)
2 try to solve the problem yourself
3 if you still need help, post a message in this forum stating:
3.1 that you need help with your homework
3.2 the exact wording of the assignment
3.3 what you have tried so far
3.4 that you really like what you have learned about Lisp so far (for extra
goodwill :-)
Good luck.
Erik
(setq passwd-readtable (copy-readtable))
(set-syntax-from-char #\: #\space passwd-readtable))
(loop for char in
'(#\tab #\space #\" #\# #\' #\( #\) #\, #\; #\\ #\` #\|) do
(set-syntax-from-char char #\A passwd-readtable))
(setq orig-readtable (copy-readtable *readtable*))
(copy-readtable passwd-readtable *readtable*)
(setq in (open "/etc/passwd" :direction :input))
However, this is bad as the readtable applies to current
code.
Is there a way to apply a different readtable to a stream?
Or, as is likely the case, am I out in the weeds?
You need to make sure that only stuff from the password file is being
read with *readtable* is bound to your customized readtable. This
works:
(defvar *passwd-readtable* (copy-readtable))
(set-syntax-from-char #\: #\space *passwd-readtable*)
(loop
for char in
'(#\tab #\space #\" #\# #\' #\( #\) #\, #\; #\\ #\` #\|)
do
(set-syntax-from-char char #\A *passwd-readtable*))
(setq in (let ((*readtable* *passwd-readtable*)
(stream (open "/etc/passwd" :direction :input)))
(read stream)))
=> root
--
Vebjorn Ljosa
You need to make sure that only stuff from the password file is being
read with *readtable* bound to your customized readtable. This works:
(defvar *passwd-readtable* (copy-readtable))
(set-syntax-from-char #\: #\space *passwd-readtable*)
(loop
for char in
'(#\tab #\space #\" #\# #\' #\( #\) #\, #\; #\\ #\` #\|)
do
Have you tried a binding *readtable* across the relevant operations?
///
--
Norway is now run by a priest from the fundamentalist Christian People's
Party, the fifth largest party representing one eighth of the electorate.
--
Carrying a Swiss Army pocket knife in Oslo, Norway, is a criminal offense.
Thanks for everyone's responses. I was thinking it would
be a handy thing to let Lisp reader handle the parsing,
specially for comma separated values with optional
quotes. This was working quite nicely until I realised
that strings are read as symbols and uppercased. Not
good for usernames and pathnames. Back to writing my
own parser.
Thanks again.
Did you read Hyperspec section 23.1.2 ?
www.xanalys.com/software_tools/reference/HyperSpec/Body/sec_23-1-2-1.html
--
> Did you read Hyperspec section 23.1.2 ?
>
> www.xanalys.com/software_tools/reference/HyperSpec/Body/sec_23-1-2-1.html
Thanks. I missed that.
Now, what happens if changes their gecos to something like
(+ 2 3) ? Lisp will be fine with that as a symbol name, right?
What about a gecos of *readtable* ? Will that get me in trouble?
Is there anything that makes this a poor method of parsing files?
Technically? Style?
Look at READTABLE-CASE. (but if you're reading passwd files you
probably want your own reader anyway).
--tim
> Now, what happens if changes their gecos to something like
> (+ 2 3) ? Lisp will be fine with that as a symbol name, right?
>
Yes.
--
BPT <b...@tunes.org> /"\ ASCII Ribbon Campaign
backronym for Linux: \ / No HTML or RTF in mail
Linux Is Not Unix X No MS-Word in mail
Meme plague ;) ---------> / \ Respect Open Standards