From: David
Subject: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <56ea2da4.0203280327.5a152717@posting.google.com>
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

From: Nils Goesche
Subject: Re: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <a7v071$o5qrc$1@ID-125440.news.dfncis.de>
In article <····························@posting.google.com>, David wrote:
> 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
From: David
Subject: Re: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <56ea2da4.0203280559.3a627ced@posting.google.com>
Thanks Nils
That's a great help.

David
From: Erik Naggum
Subject: Re: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <3226320120828579@naggum.net>
* ···········@yahoo.com (David)
| 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.
From: Nils Goesche
Subject: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <a7vfdv$oh42r$1@ID-125440.news.dfncis.de>
In article <················@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?

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9
From: Nils Goesche
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <a7vfhh$oh42r$2@ID-125440.news.dfncis.de>
In article <··············@ID-125440.news.dfncis.de>, Nils Goesche wrote:
> In article <················@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?

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9
From: Nils Goesche
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <a7vfkn$oh42r$3@ID-125440.news.dfncis.de>
In article <··············@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

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9
From: IPmonger
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <m3u1r0fx8m.fsf@validus.delamancha.org>
Nils Goesche <······@cartan.de> writes:

> In article <··············@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
········@delamancha.org
From: Nils Goesche
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <a7vjqb$o9qc6$1@ID-125440.news.dfncis.de>
In article <··············@validus.delamancha.org>, IPmonger wrote:
> Nils Goesche <······@cartan.de> writes:
> 
>> In article <··············@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)

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x42B32FC9
From: Jon Allen Boone
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <m3zo0s1ghq.fsf@validus.delamancha.org>
Nils Goesche <······@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
-- 
------------------
Jon Allen Boone
········@delamancha.org
From: Jon Allen Boone
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <m3vgbg1fkf.fsf@validus.delamancha.org>
Jon Allen Boone <········@delamancha.org> writes:

> Nils Goesche <······@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...

-jon
-- 
------------------
Jon Allen Boone
········@delamancha.org
From: Erik Naggum
Subject: Re: Reading without interning WAS: Read a line from a file, turn into a list of numbers
Date: 
Message-ID: <3226322482583350@naggum.net>
* 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.