From: Samantha Skyler
Subject: Text file to Array Help Please
Date: 
Message-ID: <7271dfd0.0205221840.41184e75@posting.google.com>
Hi everyone,

I&#8217;m a college student with a lot of free time this summer, so
I&#8217;m trying to learn LISP because I&#8217;m interested in
artificial intelligence.

Anyway,  I&#8217;m familiar with other computer programming languages.
 I&#8217;m having a problem that I hoped you boys could help me with.

I&#8217;m starting with a text file that looks like this:

10.00	2.30	12.00	FALSE
9.00	2.20	9.62	4.00
8.00	2.08	8.63	1.00
7.00	1.95	7.64	2.00
6.00	1.79	6.65	1.00
				
It&#8217;s a txt file generated by excel with tab delineation.
I would like to be able to read this into an array in lisp so it looks
like this:


#2A((10.0 9.0 8.0 7.0 6.0)
    (2.3 2.2 2.08 1.95 1.79)
    (10.0 9.62 8.63 7.64 6.65)
    (NIL 4.0 1.0 2.0 1.0))

You may notice that the vertical rows have become horizontal.

I have written a LISP program that allows me to generate the array in
LISP, if I enter the values by hand.

I would really like to have a program in LISP that can automatically
read the tab delineated text file and output the LISP array, but I
can&#8217;t figure out how to do it on my own.

Any help would be greatly appreciated
Thanks guys.

-Samantha

From: Robert Folland
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <uy9ebgtt2.fsf@circinus.no>
··············@hotmail.com (Samantha Skyler) writes:

> I&#8217;m starting with a text file that looks like this:
> 
> 10.00	2.30	12.00	FALSE
> 9.00	2.20	9.62	4.00
> 8.00	2.08	8.63	1.00
> 7.00	1.95	7.64	2.00
> 6.00	1.79	6.65	1.00
> 				
> It&#8217;s a txt file generated by excel with tab delineation.
> I would like to be able to read this into an array in lisp so it looks
> like this:
> 
> 
> #2A((10.0 9.0 8.0 7.0 6.0)
>     (2.3 2.2 2.08 1.95 1.79)
>     (10.0 9.62 8.63 7.64 6.65)
>     (NIL 4.0 1.0 2.0 1.0))

Something like this?

(defun get-excel-data (fname)
  (let ((data (make-array '(4 5))))
    (with-open-file (stream fname)
      (dotimes (i (array-dimension data 1))
	(dotimes (j (array-dimension data 0))
	  (setf (aref data j i) (read stream)))))
    data))

-Robert
From: Samantha Skyler
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <7271dfd0.0205231058.59209f64@posting.google.com>
Wow!  That worked really great.

Just one more question if you don't mind.

In line three of your program:

 (let ((data (make-array '(4 5))))

You need to give the computer the dimensions of the array.  Is there
anyway that you can have the program determin that automatically from
the text file?

Thanks again

-Samantha Skyler


Robert Folland <······@circinus.no> wrote in message news:<·············@circinus.no>...
> ··············@hotmail.com (Samantha Skyler) writes:
> 
> > I&#8217;m starting with a text file that looks like this:
> > 
> > 10.00	2.30	12.00	FALSE
> > 9.00	2.20	9.62	4.00
> > 8.00	2.08	8.63	1.00
> > 7.00	1.95	7.64	2.00
> > 6.00	1.79	6.65	1.00
> > 				
> > It&#8217;s a txt file generated by excel with tab delineation.
> > I would like to be able to read this into an array in lisp so it looks
> > like this:
> > 
> > 
> > #2A((10.0 9.0 8.0 7.0 6.0)
> >     (2.3 2.2 2.08 1.95 1.79)
> >     (10.0 9.62 8.63 7.64 6.65)
> >     (NIL 4.0 1.0 2.0 1.0))
> 
> Something like this?
> 
> (defun get-excel-data (fname)
>   (let ((data (make-array '(4 5))))
>     (with-open-file (stream fname)
>       (dotimes (i (array-dimension data 1))
> 	(dotimes (j (array-dimension data 0))
> 	  (setf (aref data j i) (read stream)))))
>     data))
> 
> -Robert
From: pizza
Subject: "Dynamically" getting data
Date: 
Message-ID: <pan.2002.05.23.19.39.27.702834.3767@parseerror.com>
On Thu, 23 May 2002 14:58:50 -0400, Samantha Skyler wrote:

> Wow!  That worked really great.
> 
> Just one more question if you don't mind.
> 
> In line three of your program:
> 
>  (let ((data (make-array '(4 5))))
> 
> You need to give the computer the dimensions of the array.  Is there
> anyway that you can have the program determin that automatically from
> the text file?
> 
> Thanks again
> 
> -Samantha Skyler
> 
> 
> Robert Folland <······@circinus.no> wrote in message
> news:<·············@circinus.no>...
>> ··············@hotmail.com (Samantha Skyler) writes:
>> 
>> > I&#8217;m starting with a text file that looks like this:
>> > 
>> > 10.00	2.30	12.00	FALSE
>> > 9.00	2.20	9.62	4.00
>> > 8.00	2.08	8.63	1.00
>> > 7.00	1.95	7.64	2.00
>> > 6.00	1.79	6.65	1.00
>> > 				
>> > It&#8217;s a txt file generated by excel with tab delineation. I
>> > would like to be able to read this into an array in lisp so it looks
>> > like this:
>> > 
>> > 
>> > #2A((10.0 9.0 8.0 7.0 6.0)
>> >     (2.3 2.2 2.08 1.95 1.79)
>> >     (10.0 9.62 8.63 7.64 6.65)
>> >     (NIL 4.0 1.0 2.0 1.0))
>> 
>> Something like this?
>> 
>> (defun get-excel-data (fname)
>>   (let ((data (make-array '(4 5))))
>>     (with-open-file (stream fname)
>>       (dotimes (i (array-dimension data 1))
>> 	(dotimes (j (array-dimension data 0))
>> 	  (setf (aref data j i) (read stream)))))
>>     data))
>> 
>> -Robert

Here's a newbie answer... it works for me in CLISP.

(defun get-excel-data (file cols)
  (let
    ((cell nil) (n 0) (table (make-array (list cols) :initial-element nil)))
    (with-open-file (str file)
      (do
        ((cell (read str nil 'eof) (read str nil 'eof)))
        ((eql cell 'eof))
        (if (eql cell 'FALSE) (setf cell nil))
        (setf (elt table (mod n cols)) (cons cell (elt table (mod n cols))))
        (incf n)
      )
      table
    )
  )
)

love,

pizza

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com
From: Kaz Kylheku
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <slrnaeqm5b.vnb.kaz@localhost.localdomain>
On 23 May 2002 11:58:50 -0700, Samantha Skyler
<··············@hotmail.com> wrote:
>Wow!  That worked really great.
>
>Just one more question if you don't mind.
>
>In line three of your program:
>
> (let ((data (make-array '(4 5))))
>
>You need to give the computer the dimensions of the array.  Is there
>anyway that you can have the program determin that automatically from
>the text file?

One way is to make a guess at some initial dimensions, and then later use the
adjust-array function to change the dimensions of the array. Obviously, you
can't know how many columns there are are until you parse an entire row of the
file.
From: Erik Naggum
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3231176581032429@naggum.net>
* Samantha Skyler
| You need to give the computer the dimensions of the array.  Is there
| anyway that you can have the program determin that automatically from
| the text file?

  Build lists of lists, row by row, then give the nested list structure you
  have constructed to make-array as the :initial-contents argument.

  The collect keyword in loop is a great way to build lists.
-- 
  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.

  70 percent of American adults do not understand the scientific process.
From: Drew McDermott
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3CEE4FBE.4010804@yale.edu>
Samantha Skyler wrote:
>  Is there
> any way that you can have the program determin that automatically from
> the text file?
> 

Here's how I would do it (and have done it in the past).  Note that this 
returns the array as an array of rows, one per line in the file.  I 
leave it as an exercise to transpose it to the array you want.  Also, 
some may quibble that it doesn't allow you to create an Nx0 array. 
Another exercise.

            -- Drew McDermott


(in-package :cl-user)

(defun readarray (filename)
    (with-open-file (grades filename :direction :input)
       (labels ((nextline ()
		  ;; File is read one line at a time.
		  (let ((str (read-line grades nil nil)))
		     (cond ((not str) '())
			   (t
			    (read-data-from-line str)))))

	       ;; Now extract the data from the string corresponding
	       ;; to the current line
	       (read-data-from-line (str)
		  (with-input-from-string (data str)
		     (let ((nums '()) n)
			(loop
			   (setq n (read data nil nil))
			   (cond ((not n)
				  (return (reverse nums))))
			   (setq nums (cons n nums))))))

	       ;; If they'are all the same length, return it, else nil.
	       (all-same-length (lines)
		  (cond ((null lines) 0)
			(t
			 (let ((linelen (length (car lines))))
			    (cond ((every (lambda (l) (= (length l) linelen))
					  (cdr lines))
				   linelen)
				  (t nil)))))))
         (let ((lines '()) line)
	   (loop
	      (setq line (nextline))
	      (cond ((null line)
		     (setq lines (reverse lines))
		     (let ((linelen (all-same-length lines)))
		        (cond (linelen
			       (return
				  (make-array (list (length lines)
						    linelen)
					      :initial-contents lines)))
			      (t
			       (error
				  "Attempt to create ragged array from ~s"
				  lines)))))
		    (t
		     (setq lines (cons line lines)))))))))
From: sv0f
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <none-2405021626230001@129.59.212.53>
In article <················@yale.edu>, Drew McDermott
<··············@yale.edu> wrote:

>Samantha Skyler wrote:
>>  Is there
>> any way that you can have the program determin that automatically from
>> the text file?
>> 
>
>Here's how I would do it (and have done it in the past).

[SNIP]

>(in-package :cl-user)
>
>(defun readarray (filename)
>    (with-open-file (grades filename :direction :input)
>       (labels ((nextline ()
>                  ;; File is read one line at a time.
>                  (let ((str (read-line grades nil nil)))
>                     (cond ((not str) '())
>                           (t
>                            (read-data-from-line str)))))
>
>               ;; Now extract the data from the string corresponding
>               ;; to the current line
>               (read-data-from-line (str)
>                  (with-input-from-string (data str)
>                     (let ((nums '()) n)
>                        (loop
>                           (setq n (read data nil nil))
>                           (cond ((not n)
>                                  (return (reverse nums))))
                                            ^^^^^^^
Won't NREVERSE work just fine because you're consing up the NUMS list
yourself?

>                           (setq nums (cons n nums))))))
>
>               ;; If they'are all the same length, return it, else nil.
>               (all-same-length (lines)
>                  (cond ((null lines) 0)
>                        (t
>                         (let ((linelen (length (car lines))))
>                            (cond ((every (lambda (l) (= (length l) linelen))
>                                          (cdr lines))
>                                   linelen)
>                                  (t nil)))))))
>         (let ((lines '()) line)
>           (loop
>              (setq line (nextline))
>              (cond ((null line)
>                     (setq lines (reverse lines))
                                   ^^^^^^^
Ditto.

>                     (let ((linelen (all-same-length lines)))
>                        (cond (linelen
>                               (return
>                                  (make-array (list (length lines)
>                                                    linelen)
>                                              :initial-contents lines)))
>                              (t
>                               (error
>                                  "Attempt to create ragged array from ~s"
>                                  lines)))))
>                    (t
>                     (setq lines (cons line lines)))))))))

Of course, you may have used REVERSE so as not to confuse the newbie.
From: David Sletten
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3CECE025.2040903@slytobias.com>
1. Learn how to use your damn editor. D&#8217;ya think that&#8217;s a 
reasonable request? I&#8217;d hope so...
2. Don't you think the term 'boys' is a bit sexist, honey?
3. 'Delineate' means to "draw or trace the outline of". Perhaps you mean 
  'delimit'?

Samantha Skyler wrote:

> Hi everyone,
> 
> I&#8217;m a college student with a lot of free time this summer, so
> I&#8217;m trying to learn LISP because I&#8217;m interested in
> artificial intelligence.
> 
> Anyway,  I&#8217;m familiar with other computer programming languages.
>  I&#8217;m having a problem that I hoped you boys could help me with.
> 
> I&#8217;m starting with a text file that looks like this:
> 
> 10.00	2.30	12.00	FALSE
> 9.00	2.20	9.62	4.00
> 8.00	2.08	8.63	1.00
> 7.00	1.95	7.64	2.00
> 6.00	1.79	6.65	1.00
> 				
> It&#8217;s a txt file generated by excel with tab delineation.
> I would like to be able to read this into an array in lisp so it looks
> like this:
> 
> 
> #2A((10.0 9.0 8.0 7.0 6.0)
>     (2.3 2.2 2.08 1.95 1.79)
>     (10.0 9.62 8.63 7.64 6.65)
>     (NIL 4.0 1.0 2.0 1.0))
> 
> You may notice that the vertical rows have become horizontal.
> 
> I have written a LISP program that allows me to generate the array in
> LISP, if I enter the values by hand.
> 
> I would really like to have a program in LISP that can automatically
> read the tab delineated text file and output the LISP array, but I
> can&#8217;t figure out how to do it on my own.
> 
> Any help would be greatly appreciated
> Thanks guys.
> 
> -Samantha
> 
From: Coby Beck
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <sh8H8.128396$xS2.10005006@news1.calgary.shaw.ca>
"David Sletten" <·····@slytobias.com> wrote in message
·····················@slytobias.com...
> 1. Learn how to use your damn editor. D&#8217;ya think that&#8217;s a
> reasonable request? I&#8217;d hope so...
> 2. Don't you think the term 'boys' is a bit sexist, honey?
> 3. 'Delineate' means to "draw or trace the outline of". Perhaps you mean
>   'delimit'?
>

I think that was an incredibly rude and pointless post.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Samantha Skyler
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3fd88790.0205231615.739f1b30@posting.google.com>
David,

I'm sorry about the 'boys'.

I'm the only female I've ever come across in my college life who was
seriously intrested in computer science and computer programing.  I've
been surrounded by men when ever I try to find out more than I am able
to learn on my own with reguard to these topics.

I'm sorry for offending you.

-Samantha

David Sletten <·····@slytobias.com> wrote in message news:<················@slytobias.com>...
> 1. Learn how to use your damn editor. D&#8217;ya think that&#8217;s a 
> reasonable request? I&#8217;d hope so...
> 2. Don't you think the term 'boys' is a bit sexist, honey?
> 3. 'Delineate' means to "draw or trace the outline of". Perhaps you mean 
>   'delimit'?
> 
> Samantha Skyler wrote:
> 
> > Hi everyone,
> > 
> > I&#8217;m a college student with a lot of free time this summer, so
> > I&#8217;m trying to learn LISP because I&#8217;m interested in
> > artificial intelligence.
> > 
> > Anyway,  I&#8217;m familiar with other computer programming languages.
> >  I&#8217;m having a problem that I hoped you boys could help me with.
> > 
> > I&#8217;m starting with a text file that looks like this:
> > 
> > 10.00	2.30	12.00	FALSE
> > 9.00	2.20	9.62	4.00
> > 8.00	2.08	8.63	1.00
> > 7.00	1.95	7.64	2.00
> > 6.00	1.79	6.65	1.00
> > 				
> > It&#8217;s a txt file generated by excel with tab delineation.
> > I would like to be able to read this into an array in lisp so it looks
> > like this:
> > 
> > 
> > #2A((10.0 9.0 8.0 7.0 6.0)
> >     (2.3 2.2 2.08 1.95 1.79)
> >     (10.0 9.62 8.63 7.64 6.65)
> >     (NIL 4.0 1.0 2.0 1.0))
> > 
> > You may notice that the vertical rows have become horizontal.
> > 
> > I have written a LISP program that allows me to generate the array in
> > LISP, if I enter the values by hand.
> > 
> > I would really like to have a program in LISP that can automatically
> > read the tab delineated text file and output the LISP array, but I
> > can&#8217;t figure out how to do it on my own.
> > 
> > Any help would be greatly appreciated
> > Thanks guys.
> > 
> > -Samantha
> >
From: David Sletten
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3CEDAECB.9040009@slytobias.com>
Samantha,

I appreciate your apology although I was not looking for one. I merely 
wanted you to think about what you were saying. I have a wife, a sister, 
and two daughters. I want them to be afforded every opportunity that I 
have enjoyed as a male and not be pre-judged based on their sex.

I personally don't mind the term 'boys' just as I don't think that 
referring to a group of women as 'girls' or 'gals' is particularly 
offensive. The bothersome aspect is the assumption inherent in your 
statement that there are no women in this community worthy of responding 
to your question. As you point out, of course, the reality of the matter 
is that the percentage of women working with computers (most technical 
fields it appears) is quite small. Be that as it may, I have 3 Lisp 
textbooks on my shelf that were written by women. And the 2 that I've 
read so far are very good. These women know a helluva lot more about 
Lisp than I do.

Now, having said that, I should apologize as well for coming across so 
cranky. My goal was not to drive away one of the few women who do 
participate here. I am sorry.

If you are seriously interested in computer science and computer 
programming, you've come to the right place. Common Lisp is a great 
language. It's a big language, and will require a lot of work to master 
(remember the HyperSpec is your friend!). But if you demonstrate that 
you are honestly trying to understand your problem there are many here 
(more experienced than I) who will gladly help you.

Sincerely,
David Sletten

P.S. Thank you for fixing that apostrophe problem! :)

Samantha Skyler wrote:

> David,
> 
> I'm sorry about the 'boys'.
> 
> I'm the only female I've ever come across in my college life who was
> seriously intrested in computer science and computer programing.  I've
> been surrounded by men when ever I try to find out more than I am able
> to learn on my own with reguard to these topics.
> 
> I'm sorry for offending you.
> 
> -Samantha
> 
From: Bulent Murtezaoglu
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <87it5exmdd.fsf@nkapi.internal>
>>>>> "DS" == David Sletten <·····@slytobias.com> writes:

    DS> ... Be that as it may, I have 3
    DS> Lisp textbooks on my shelf that were written by women. And the
    DS> 2 that I've read so far are very good. These women know a
    DS> helluva lot more about Lisp than I do. [...]

I know about and like Keene's CLOS book, who are the other two?

cheers,

BM
From: David Sletten
Subject: Re: Text file to Array Help Please
Date: 
Message-ID: <3CEDB84B.1070609@slytobias.com>
Unfortunately I believe these are both hard to find:

A Programmer's Guide to Common Lisp (Deborah Tatar)
Understanding CLOS (Jo A. Lawless/Molly M. Miller) [Haven't read yet]

After further thought, I also remembered that Julie Sussman is a 
co-author of SICP.

Bulent Murtezaoglu wrote:

>>>>>>"DS" == David Sletten <·····@slytobias.com> writes:
>>>>>>
> 
>     DS> ... Be that as it may, I have 3
>     DS> Lisp textbooks on my shelf that were written by women. And the
>     DS> 2 that I've read so far are very good. These women know a
>     DS> helluva lot more about Lisp than I do. [...]
> 
> I know about and like Keene's CLOS book, who are the other two?
> 
> cheers,
> 
> BM
>