Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read a line from a file, turn into a list of numbers

9 views
Skip to first unread message

David

unread,
Mar 28, 2002, 6:27:23 AM3/28/02
to
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

Nils Goesche

unread,
Mar 28, 2002, 6:53:05 AM3/28/02
to

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

David

unread,
Mar 28, 2002, 8:59:26 AM3/28/02
to
Thanks Nils
That's a great help.

David

Erik Naggum

unread,
Mar 28, 2002, 11:01:46 AM3/28/02
to
* wild_...@yahoo.com (David)

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.

Nils Goesche

unread,
Mar 28, 2002, 11:12:47 AM3/28/02
to
In article <32263201...@naggum.net>, Erik Naggum wrote:

> 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?

Nils Goesche

unread,
Mar 28, 2002, 11:14:41 AM3/28/02
to
In article <a7vfdv$oh42r$1...@ID-125440.news.dfncis.de>, Nils Goesche wrote:
> In article <32263201...@naggum.net>, Erik Naggum wrote:
>
>> 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?

Nils Goesche

unread,
Mar 28, 2002, 11:16:23 AM3/28/02
to
In article <a7vfhh$oh42r$2...@ID-125440.news.dfncis.de>, Nils Goesche wrote:
>
> Aargh, I guess it wouldn't work: What if the file contains cl:foo?

Or better: cl::foo

Erik Naggum

unread,
Mar 28, 2002, 11:41:07 AM3/28/02
to
* 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.

IPmonger

unread,
Mar 28, 2002, 11:55:21 AM3/28/02
to
Nils Goesche <car...@cartan.de> writes:

> In article <a7vfhh$oh42r$2...@ID-125440.news.dfncis.de>, Nils Goesche wrote:
>>
>> 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
ipmo...@delamancha.org

Nils Goesche

unread,
Mar 28, 2002, 12:27:39 PM3/28/02
to
In article <m3u1r0f...@validus.delamancha.org>, IPmonger wrote:
> Nils Goesche <car...@cartan.de> writes:
>
>> In article <a7vfhh$oh42r$2...@ID-125440.news.dfncis.de>, Nils Goesche wrote:
>>>
>>> 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)

Jon Allen Boone

unread,
Mar 28, 2002, 5:20:49 PM3/28/02
to
Nils Goesche <car...@cartan.de> writes:

>> 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 Allen Boone

unread,
Mar 28, 2002, 5:44:27 PM3/28/02
to
Jon Allen Boone <ipmo...@delamancha.org> writes:

> Nils Goesche <car...@cartan.de> writes:
>
>>> 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...

0 new messages