Author |
Message |
Davi #1 / 13
|
 Read a line from a file, turn into a list of numbers
Hi I'm trying to learn lisp and am struggling a bit over doing this seemingly simple task. I want to read a line from a file and convert it into a list of numbers. So far I have only managed to convert it into a list of characters and can't figure out how to get a list of numbers (the numbers are separated by spaces in the file) from this. Can anyone point me in the right function direction. Thanks David
|
Mon, 13 Sep 2004 19:27:23 GMT |
|
 |
Nils Goesch #2 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
> I'm trying to learn lisp and am struggling a bit over doing this > seemingly simple task. I want to read a line from a file and convert > it into a list of numbers. So far I have only managed to convert it > into a list of characters and can't figure out how to get a list of > numbers (the numbers are separated by spaces in the file) from this. > Can anyone point me in the right function direction.
There are numerous ways of doing this; here are two quick ones: (defun read-number-line () (with-open-file (stream "/tmp/foo") (let ((line (read-line stream))) (with-input-from-string (stream line) (loop for num = (read stream nil nil) while num collect num))))) or (defun read-number-line () (with-open-file (stream "/tmp/foo") (let ((line (read-line stream))) (read-from-string (concatenate 'string "(" line ")"))))) Both have serious drawbacks, but I hope you find something useful in them. Also lookup *READ-EVAL*, READ and PARSE-INTEGER to find out what you could use. Also, HANDLER-CASE might be useful. Hell, it's really hard to tell without further information :-) Regards, -- Nils Goesche "Don't ask for whom the <CTRL-G> tolls." PGP key ID 0x42B32FC9
|
Mon, 13 Sep 2004 19:53:05 GMT |
|
 |
Davi #3 / 13
|
 Read a line from a file, turn into a list of numbers
Thanks Nils That's a great help. David
|
Mon, 13 Sep 2004 21:59:26 GMT |
|
 |
Erik Naggu #4 / 13
|
 Read a line from a file, turn into a list of numbers
| I'm trying to learn lisp and am struggling a bit over doing this | seemingly simple task. I want to read a line from a file and convert it | into a list of numbers. So far I have only managed to convert it into a | list of characters and can't figure out how to get a list of numbers (the | numbers are separated by spaces in the file) from this. Can anyone point | me in the right function direction. In addition to Nils's suggestions, look into the reader variable *read-default-float-format* if you have floating-point numbers. Also, please note that when you use the general reader, any symbol with a syntax error relative to the Common Lisp syntax, will be returned as a symbol. It might be a good idea to set up your own readtable to exclude irrelevant data types, too. Unfortunately, there is no standard way to avoid interning new symbols. (I wish there were at least some way to control the reader's willingness to make symbols.) /// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.
|
Tue, 14 Sep 2004 00:01:46 GMT |
|
 |
Nils Goesch #5 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
> Unfortunately, there is no standard way to avoid interning new > symbols. (I wish there were at least some way to control the reader's > willingness to make symbols.)
I've been wondering about this, recently. Maybe one could make a new package, bind *package* to the new package, read the file in, and then delete the temporary package again. Would that work? Is there a better way? Regards, -- Nils Goesche "Don't ask for whom the <CTRL-G> tolls." PGP key ID 0x42B32FC9
|
Tue, 14 Sep 2004 00:12:47 GMT |
|
 |
Nils Goesch #6 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
>> Unfortunately, there is no standard way to avoid interning new >> symbols. (I wish there were at least some way to control the reader's >> willingness to make symbols.) > I've been wondering about this, recently. Maybe one could make a new > package, bind *package* to the new package, read the file in, and > then delete the temporary package again. Would that work? Is > there a better way?
Aargh, I guess it wouldn't work: What if the file contains cl:foo? Regards, -- Nils Goesche "Don't ask for whom the <CTRL-G> tolls." PGP key ID 0x42B32FC9
|
Tue, 14 Sep 2004 00:14:41 GMT |
|
 |
Nils Goesch #7 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
> Aargh, I guess it wouldn't work: What if the file contains cl:foo?
Or better: cl::foo Regards, -- Nils Goesche "Don't ask for whom the <CTRL-G> tolls." PGP key ID 0x42B32FC9
|
Tue, 14 Sep 2004 00:16:23 GMT |
|
 |
Erik Naggu #8 / 13
|
 Read a line from a file, turn into a list of numbers
* Nils Goesche | I've been wondering about this, recently. Maybe one could make a new | package, bind *package* to the new package, read the file in, and then | delete the temporary package again. Would that work? Is there a better | way? A _bad_ way is to bind *package* to a non-package and hope to catch the error without the whole system blowing up. This is a Jackass episode. /// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.
|
Tue, 14 Sep 2004 00:41:07 GMT |
|
 |
IPmonge #9 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
>> Aargh, I guess it wouldn't work: What if the file contains cl:foo? > Or better: cl::foo
Can't you lock packages so that new symbols can't be created in them? -jon -- ------------------ Jon Allen Boone
|
Tue, 14 Sep 2004 00:55:21 GMT |
|
 |
Nils Goesch #10 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
>>> Aargh, I guess it wouldn't work: What if the file contains cl:foo? >> Or better: cl::foo > Can't you lock packages so that new symbols can't be created in them?
Yes, that would be nice. How? (I don't know any way to do that) Regards, -- Nils Goesche "Don't ask for whom the <CTRL-G> tolls." PGP key ID 0x42B32FC9
|
Tue, 14 Sep 2004 01:27:39 GMT |
|
 |
Jon Allen Boon #11 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
>> Can't you lock packages so that new symbols can't be created in them? > Yes, that would be nice. How? (I don't know any way to do that)
It's probably not portable - I couldn't find anything in the HyperSpec that mentions this - but CLISP has a function (package-lock ). I imagine others may too... -jon -- ------------------ Jon Allen Boone
|
Tue, 14 Sep 2004 06:20:49 GMT |
|
 |
Jon Allen Boon #12 / 13
|
 Read a line from a file, turn into a list of numbers
Quote:
>>> Can't you lock packages so that new symbols can't be created in them? >> Yes, that would be nice. How? (I don't know any way to do that) > It's probably not portable - I couldn't find anything in the HyperSpec > that mentions this - but CLISP has a function (package-lock ). I imagine > others may too...
And, now that I've tried to use it, it doesn't do what I thought. :-( Ok, my bad, next time I'll try it first... -jon -- ------------------ Jon Allen Boone
|
Tue, 14 Sep 2004 06:44:27 GMT |
|
|