From: ··············@web.de
Subject: Reading file and work on the data
Date: 
Message-ID: <1178548998.883423.183000@y80g2000hsf.googlegroups.com>
Hi!

I have a text file to read in, its data consists of several rows of 0
and 1 seperated by -for example- tabs (\t):
0 \t  1 \t  1  \t   0 ... \n
1 \t  0 \t  1  \t   0 ....\n
....

With following code  I can read and format the data:
(let ((str (open "testdata.txt")))
   (when str
    (loop for line = (read-line str nil)
          while line do (format t "~a~%" line)))
  (close str))

But how can I get the single(!) zeros or ones? With the code mentioned
above I can only read a whole line, not the single characters/
numbers.
The aim is to construct a data structure consisting of the numbers,
for example:
(setf (aref a 0 0)  (make-struct :x "X1" :value 0))
(setf (aref a 0 1)  (make-struct :x "X2" :value 1))
(setf (aref a 0 2)  (make-struct :x "X3" :value 1))
....

Thanks!

Daniela
Thank you!

From: dpapathanasiou
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <1178549871.120946.224740@y80g2000hsf.googlegroups.com>
On May 7, 10:43 am, ··············@web.de wrote:
> Hi!
>
> I have a text file to read in, its data consists of several rows of 0
> and 1 seperated by -for example- tabs (\t):
> 0 \t  1 \t  1  \t   0 ... \n
> 1 \t  0 \t  1  \t   0 ....\n
> ....
>
> With following code  I can read and format the data:
> (let ((str (open "testdata.txt")))
>    (when str
>     (loop for line = (read-line str nil)
>           while line do (format t "~a~%" line)))
>   (close str))
>
> But how can I get the single(!) zeros or ones? With the code mentioned
> above I can only read a whole line, not the single characters/
> numbers.
> The aim is to construct a data structure consisting of the numbers,
> for example:
> (setf (aref a 0 0)  (make-struct :x "X1" :value 0))
> (setf (aref a 0 1)  (make-struct :x "X2" :value 1))
> (setf (aref a 0 2)  (make-struct :x "X3" :value 1))
> ....
>
> Thanks!
>
> Daniela
> Thank you!

If each line of text in the file is the same length, you can read it
into memory with (read-sequence) and use (aref) to pull out the
attributes you want.

(read-sequence) returns a one-dimensional array, but conceptually, you
can treat it as a 2-dimensional array, and calculate the (aref) start
and stop positions of the attributes you want by multiplying the line-
number by the (fixed) line size.
From: Pascal Costanza
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <5a8thjF2ncedeU1@mid.individual.net>
dpapathanasiou wrote:
> On May 7, 10:43 am, ··············@web.de wrote:
>> Hi!
>>
>> I have a text file to read in, its data consists of several rows of 0
>> and 1 seperated by -for example- tabs (\t):
>> 0 \t  1 \t  1  \t   0 ... \n
>> 1 \t  0 \t  1  \t   0 ....\n
>> ....
>>
>> With following code  I can read and format the data:
>> (let ((str (open "testdata.txt")))
>>    (when str
>>     (loop for line = (read-line str nil)
>>           while line do (format t "~a~%" line)))
>>   (close str))
>>
>> But how can I get the single(!) zeros or ones? With the code mentioned
>> above I can only read a whole line, not the single characters/
>> numbers.
>> The aim is to construct a data structure consisting of the numbers,
>> for example:
>> (setf (aref a 0 0)  (make-struct :x "X1" :value 0))
>> (setf (aref a 0 1)  (make-struct :x "X2" :value 1))
>> (setf (aref a 0 2)  (make-struct :x "X3" :value 1))
>> ....
>>
>> Thanks!
>>
>> Daniela
>> Thank you!
> 
> If each line of text in the file is the same length, you can read it
> into memory with (read-sequence) and use (aref) to pull out the
> attributes you want.
> 
> (read-sequence) returns a one-dimensional array, but conceptually, you
> can treat it as a 2-dimensional array, and calculate the (aref) start
> and stop positions of the attributes you want by multiplying the line-
> number by the (fixed) line size.

...or you use a displaced array on top and let Common Lisp do the job 
for you. See the description of the displaced-to keyword for make-array.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Rainer Joswig
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <joswig-FBFF1D.17061507052007@news-europe.giganews.com>
In article <························@y80g2000hsf.googlegroups.com>,
 ··············@web.de wrote:

> Hi!
> 
> I have a text file to read in, its data consists of several rows of 0
> and 1 seperated by -for example- tabs (\t):
> 0 \t  1 \t  1  \t   0 ... \n
> 1 \t  0 \t  1  \t   0 ....\n
> ....
> 
> With following code  I can read and format the data:
> (let ((str (open "testdata.txt")))
>    (when str
>     (loop for line = (read-line str nil)
>           while line do (format t "~a~%" line)))
>   (close str))

Try WITH-OPEN-FILE

> 
> But how can I get the single(!) zeros or ones? With the code mentioned
> above I can only read a whole line, not the single characters/
> numbers.

A line is a string

"0-1-0-1"

You get elements of a string with AREF:

(aref "0-1-0-1" 2)

Elements of strings are characters.

(case (aref "0-1-0-1" 0)
   (#\0 0)
   (#\1 1))

All you need to do is to look at the characters in
the string and you get the corresponding number.

A LOOP will let you iterate of the string elements:

(let ((string "0-1-0-1-1"))
   (loop for i from 0 by 2 below (length string)
         do (princ (aref string i))))

And so on.

Does that help?

> The aim is to construct a data structure consisting of the numbers,
> for example:
> (setf (aref a 0 0)  (make-struct :x "X1" :value 0))
> (setf (aref a 0 1)  (make-struct :x "X2" :value 1))
> (setf (aref a 0 2)  (make-struct :x "X3" :value 1))
> ....
> 
> Thanks!
> 
> Daniela
> Thank you!

-- 
http://lispm.dyndns.org
From: ··············@web.de
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <1178567015.834010.140610@u30g2000hsc.googlegroups.com>
> A line is a string
>
> "0-1-0-1"
>
> You get elements of a string with AREF:
>
> (aref "0-1-0-1" 2)


If I get an element of the string, its type is a STANDARD-CHAR:
> #\1
How can I convert it into a number?

Thank you so long!
From: ··············@web.de
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <1178568185.599557.39500@y80g2000hsf.googlegroups.com>
> If I get an element of the string, its type is a STANDARD-CHAR:> #\1
>
> How can I convert it into a number?


(digit-char-p var) :-)
From: Rainer Joswig
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <joswig-18C362.22121207052007@news-europe.giganews.com>
In article <·······················@y80g2000hsf.googlegroups.com>,
 ··············@web.de wrote:

> > If I get an element of the string, its type is a STANDARD-CHAR:> #\1
> >
> > How can I convert it into a number?
> 
> 
> (digit-char-p var) :-)

Right!

Here is the documentation for it:

http://www.lispworks.com/documentation/HyperSpec/Body/f_digi_1.htm#digit-char-p

-- 
http://lispm.dyndns.org
From: Dan Bensen
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <f1o2po$rfi$1@wildfire.prairienet.org>
··············@web.de wrote:
> If I get an element of the string, its type is a STANDARD-CHAR:
>> #\1
> How can I convert it into a number?

The function PARSE-INTEGER expects a string, even if it's only
one character long.  So what you need is the one-character
substring of your input string.  You can pull it out of the
original string, or you can use the original string and
specify :START and :END values in PARSE-INTEGER.

-- 
Dan
www.prairienet.org/~dsb/
From: Rainer Joswig
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <joswig-CDDAE8.22011207052007@news-europe.giganews.com>
In article <························@u30g2000hsc.googlegroups.com>,
 ··············@web.de wrote:

> > A line is a string
> >
> > "0-1-0-1"
> >
> > You get elements of a string with AREF:
> >
> > (aref "0-1-0-1" 2)
> 
> 
> If I get an element of the string, its type is a STANDARD-CHAR:
> > #\1
> How can I convert it into a number?
> 
> Thank you so long!

Did you see this part:

(case (aref "0-1-0-1" 2)
    (#\0 0)
    (#\1 1))

If it is a character 0 then return the number zero.

You can also use the character code:
(char-code #\1) -> 49

Rainer Joswig

-- 
http://lispm.dyndns.org
From: Pascal Bourguignon
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <87wszk64nm.fsf@thalassa.lan.informatimago.com>
··············@web.de writes:

> Hi!
>
> I have a text file to read in, its data consists of several rows of 0
> and 1 seperated by -for example- tabs (\t):
> 0 \t  1 \t  1  \t   0 ... \n
> 1 \t  0 \t  1  \t   0 ....\n
> ....
>
> With following code  I can read and format the data:
> (let ((str (open "testdata.txt")))
>    (when str
>     (loop for line = (read-line str nil)
>           while line do (format t "~a~%" line)))
>   (close str))
>
> But how can I get the single(!) zeros or ones? With the code mentioned
> above I can only read a whole line, not the single characters/
> numbers.
> The aim is to construct a data structure consisting of the numbers,
> for example:
> (setf (aref a 0 0)  (make-struct :x "X1" :value 0))
> (setf (aref a 0 1)  (make-struct :x "X2" :value 1))
> (setf (aref a 0 2)  (make-struct :x "X3" :value 1))
> ....
>
> Thanks!

You may also use the lisp reader. TAB, when it exists, is considered a
whitespace.  

The question is whether you have the same number of bits (in Common
Lisp, the type BIT is defined as (integer 1) = (member 0 1)), in which
case you can just write:

(defun read-bits (some-width some-height)
  (let* ((width  some-width)
         (height some-height)
         (bits (make-array (list height width) :element-type 'bit 
                                               :initial-element 0)))
    (dotimes (j height)
      (dotimes (i width)
        (when (plusp (read))
           (setf (aref bits j i) 1))))
    bits))

for example:

(with-input-from-string (*standard-input*
 "0 	  1 	  1  	   0
1 	  0 	  1  	   0
")
(read-bits 4 2))

--> #2A(#*0110 #*1010)





-- 
__Pascal_Bourguignon__               _  Software patents are endangering
()  ASCII ribbon against html email (o_ the computer industry all around
/\  1962:DO20I=1.100                //\ the world http://lpf.ai.mit.edu/
    2001:my($f)=`fortune`;          V_/   http://petition.eurolinux.org/
From: Pascal Bourguignon
Subject: Re: Reading file and work on the data
Date: 
Message-ID: <87sla864i3.fsf@thalassa.lan.informatimago.com>
··············@web.de writes:

> Hi!
>
> I have a text file to read in, its data consists of several rows of 0
> and 1 seperated by -for example- tabs (\t):
> 0 \t  1 \t  1  \t   0 ... \n
> 1 \t  0 \t  1  \t   0 ....\n
> ....
>
> With following code  I can read and format the data:
> (let ((str (open "testdata.txt")))
>    (when str
>     (loop for line = (read-line str nil)
>           while line do (format t "~a~%" line)))
>   (close str))
>
> But how can I get the single(!) zeros or ones? With the code mentioned
> above I can only read a whole line, not the single characters/
> numbers.
> The aim is to construct a data structure consisting of the numbers,
> for example:
> (setf (aref a 0 0)  (make-struct :x "X1" :value 0))
> (setf (aref a 0 1)  (make-struct :x "X2" :value 1))
> (setf (aref a 0 2)  (make-struct :x "X3" :value 1))
> ....

I'd add, using the lisp reader, you can also play tricks like this to
let it do even more work for you:

(with-input-from-string (*standard-input*
                          " 0 	  1 	  1  	   0 ")
  (make-array 4 
         :element-type 'bit
         :initial-contents (read-from-string 
                               (concatenate 'string "(" (read-line) ")"))))

--> #*0110

but in general this wouldn't be more efficient than writting your own
loop, since you duplicate the buffer read, and cons a lot more space
(to hold the initial-content list).  But for quick and dirty stuff
it's good to know.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

HANDLE WITH EXTREME CARE: This product contains minute electrically
charged particles moving at velocities in excess of five hundred
million miles per hour.