From: Volkan YAZICI
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <f85bcc2b-7531-411e-8008-27013f5f0a57@8g2000hse.googlegroups.com>
On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
> Hello, I have a txt file like below: test.txt (only showing a toy
> example of 3 cols and 5 rows but there may be 1000s of rows).
> jan     sun     12
> feb     mon     14
> mar     fri     23
> aug     sat     3
> jun     tue     15

READ-LINE an CL-PPCRE are your friends.

CL-USER> (with-open-file (in "/tmp/
data.txt")
           (loop for line = (read-line in nil
nil)
                 while line collect (cl-ppcre:split "\\s+" line)))
(("jan" "sun" "12")
 ("feb" "mon" "14")
 ("mar" "fri" "23")
 ("aug" "sat"
"3")
 ("jun" "tue" "15"))


Regards.

From: William James
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <20c5b42e-64a1-4d49-aa47-fbce82808506@z66g2000hsc.googlegroups.com>
On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>
> > Hello, I have a txt file like below: test.txt (only showing a toy
> > example of 3 cols and 5 rows but there may be 1000s of rows).
> > jan     sun     12
> > feb     mon     14
> > mar     fri     23
> > aug     sat     3
> > jun     tue     15
>
> READ-LINE an CL-PPCRE are your friends.
>
> CL-USER> (with-open-file (in "/tmp/
> data.txt")
>            (loop for line = (read-line in nil
> nil)
>                  while line collect (cl-ppcre:split "\\s+" line)))
> (("jan" "sun" "12")
>  ("feb" "mon" "14")
>  ("mar" "fri" "23")
>  ("aug" "sat"
> "3")
>  ("jun" "tue" "15"))

He wanted a list for each column.

Ruby:

IO.readlines( "data2" ).map{|s| s.split }.transpose
    ==>[["jan", "feb", "mar", "aug", "jun"],
  ["sun", "mon", "fri", "sat", "tue"],
  ["12", "14", "23", "3", "15"]]
From: Pascal J. Bourguignon
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <87od2s9zoj.fsf@hubble.informatimago.com>
William James <·········@yahoo.com> writes:

> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
>> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>>
>> > Hello, I have a txt file like below: test.txt (only showing a toy
>> > example of 3 cols and 5 rows but there may be 1000s of rows).
>> > jan     sun     12
>> > feb     mon     14
>> > mar     fri     23
>> > aug     sat     3
>> > jun     tue     15
>>
>> READ-LINE an CL-PPCRE are your friends.
>>
>> CL-USER> (with-open-file (in "/tmp/
>> data.txt")
>>            (loop for line = (read-line in nil
>> nil)
>>                  while line collect (cl-ppcre:split "\\s+" line)))
>> (("jan" "sun" "12")
>>  ("feb" "mon" "14")
>>  ("mar" "fri" "23")
>>  ("aug" "sat"
>> "3")
>>  ("jun" "tue" "15"))
>
> He wanted a list for each column.
>
> Ruby:
>
> IO.readlines( "data2" ).map{|s| s.split }.transpose
>     ==>[["jan", "feb", "mar", "aug", "jun"],
>   ["sun", "mon", "fri", "sat", "tue"],
>   ["12", "14", "23", "3", "15"]]

ENEPA


Try rather this:

(((IO.readlines "data2").map{|s| (s.split) }).transpose)
--> [["jan", "feb", "mar", "aug", "jun"],
     ["sun", "mon", "fri", "sat", "tue"],
     ["12", "14", "23", "3", "15"]]


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

ATTENTION: Despite any other listing of product contents found
herein, the consumer is advised that, in actuality, this product
consists of 99.9999999999% empty space.
From: William James
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <acf82e47-2d0b-4995-9bda-0fff6e9e4345@d45g2000hsc.googlegroups.com>
On Sep 13, 4:13 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> William James <·········@yahoo.com> writes:
> > On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
> >> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>
> >> > Hello, I have a txt file like below: test.txt (only showing a toy
> >> > example of 3 cols and 5 rows but there may be 1000s of rows).
> >> > jan     sun     12
> >> > feb     mon     14
> >> > mar     fri     23
> >> > aug     sat     3
> >> > jun     tue     15
>
> >> READ-LINE an CL-PPCRE are your friends.
>
> >> CL-USER> (with-open-file (in "/tmp/
> >> data.txt")
> >>            (loop for line = (read-line in nil
> >> nil)
> >>                  while line collect (cl-ppcre:split "\\s+" line)))
> >> (("jan" "sun" "12")
> >>  ("feb" "mon" "14")
> >>  ("mar" "fri" "23")
> >>  ("aug" "sat"
> >> "3")
> >>  ("jun" "tue" "15"))
>
> > He wanted a list for each column.
>
> > Ruby:
>
> > IO.readlines( "data2" ).map{|s| s.split }.transpose
> >     ==>[["jan", "feb", "mar", "aug", "jun"],
> >   ["sun", "mon", "fri", "sat", "tue"],
> >   ["12", "14", "23", "3", "15"]]
>
> ENEPA
>
> Try rather this:
>
> (((IO.readlines "data2").map{|s| (s.split) }).transpose)
> --> [["jan", "feb", "mar", "aug", "jun"],
>      ["sun", "mon", "fri", "sat", "tue"],
>      ["12", "14", "23", "3", "15"]]
>

It works!

I suppose that Lispers feel insecure without
their comforting parentheses.

--
The only good bureaucrat is one with a pistol at his
head.  Put it in his hand and it's good-bye to the Bill
of Rights.
  --- H.L. Mencken
From: John Thingstad
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <op.uhe26a2hut4oq5@pandora.alfanett.no>
P� Sat, 13 Sep 2008 10:57:36 +0200, skrev William James  
<·········@yahoo.com>:

> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
>> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>>
>> > Hello, I have a txt file like below: test.txt (only showing a toy
>> > example of 3 cols and 5 rows but there may be 1000s of rows).
>> > jan     sun     12
>> > feb     mon     14
>> > mar     fri     23
>> > aug     sat     3
>> > jun     tue     15
>>
>> READ-LINE an CL-PPCRE are your friends.
>>
>> CL-USER> (with-open-file (in "/tmp/
>> data.txt")
>>            (loop for line = (read-line in nil
>> nil)
>>                  while line collect (cl-ppcre:split "\\s+" line)))
>> (("jan" "sun" "12")
>>  ("feb" "mon" "14")
>>  ("mar" "fri" "23")
>>  ("aug" "sat"
>> "3")
>>  ("jun" "tue" "15"))
>
> He wanted a list for each column.
>
> Ruby:
>
> IO.readlines( "data2" ).map{|s| s.split }.transpose
>     ==>[["jan", "feb", "mar", "aug", "jun"],
>   ["sun", "mon", "fri", "sat", "tue"],
>   ["12", "14", "23", "3", "15"]]

This sort of fancy hack if fine for mastrubation but useless in real code  
where ugly details like error handeling get in the way. In this sence ruby  
is like perl. Favoring quick hacks over robust design. Works for small  
problems, a eventual nightmare for large ones.

--------------
John Thingstad
From: William James
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <9c8eb489-849c-4a05-8610-6970e93b6ab7@f63g2000hsf.googlegroups.com>
On Sep 13, 6:21 am, "John Thingstad" <·······@online.no> wrote:
> På Sat, 13 Sep 2008 10:57:36 +0200, skrev William James
> <·········@yahoo.com>:
>
> > On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
> >> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>
> >> > Hello, I have a txt file like below: test.txt (only showing a toy
> >> > example of 3 cols and 5 rows but there may be 1000s of rows).
> >> > jan     sun     12
> >> > feb     mon     14
> >> > mar     fri     23
> >> > aug     sat     3
> >> > jun     tue     15
>
> >> READ-LINE an CL-PPCRE are your friends.
>
> >> CL-USER> (with-open-file (in "/tmp/
> >> data.txt")
> >>            (loop for line = (read-line in nil
> >> nil)
> >>                  while line collect (cl-ppcre:split "\\s+" line)))
> >> (("jan" "sun" "12")
> >>  ("feb" "mon" "14")
> >>  ("mar" "fri" "23")
> >>  ("aug" "sat"
> >> "3")
> >>  ("jun" "tue" "15"))
>
> > He wanted a list for each column.
>
> > Ruby:
>
> > IO.readlines( "data2" ).map{|s| s.split }.transpose
> >     ==>[["jan", "feb", "mar", "aug", "jun"],
> >   ["sun", "mon", "fri", "sat", "tue"],
> >   ["12", "14", "23", "3", "15"]]
>
> This sort of fancy hack if fine for mastrubation but useless in real code
> where ugly details like error handeling get in the way. In this sence ruby
> is like perl. Favoring quick hacks over robust design. Works for small
> problems, a eventual nightmare for large ones.

You will always have the mentality of a COBOL
programmer, and you will will always be proud
of it.
From: Alberto Riva
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <gap4ef$cs1m$1@usenet.osg.ufl.edu>
William James wrote:
> On Sep 13, 6:21 am, "John Thingstad" <·······@online.no> wrote:
>> P� Sat, 13 Sep 2008 10:57:36 +0200, skrev William James
>> <·········@yahoo.com>:
>>
>>> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
[...]
>>> Ruby:
>>> IO.readlines( "data2" ).map{|s| s.split }.transpose
>>>     ==>[["jan", "feb", "mar", "aug", "jun"],
>>>   ["sun", "mon", "fri", "sat", "tue"],
>>>   ["12", "14", "23", "3", "15"]]
>> This sort of fancy hack if fine for mastrubation but useless in real code
>> where ugly details like error handeling get in the way. In this sence ruby
>> is like perl. Favoring quick hacks over robust design. Works for small
>> problems, a eventual nightmare for large ones.
> 
> You will always have the mentality of a COBOL
> programmer, and you will will always be proud
> of it.

Well, if the "mentality of a COBOL programmer" means favoring robust 
design and good debugging practices, then it's nothing to be ashamed of.

Anyway, I think the biggest problem with the above Ruby code is the use 
of transpose. Unless Ruby somehow manages to optimize it, it strikes me 
as a highly inefficient solution.

Alberto
From: André Thieme
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <gaq5og$dp3$1@registered.motzarella.org>
Alberto Riva schrieb:
> William James wrote:
>> On Sep 13, 6:21 am, "John Thingstad" <·······@online.no> wrote:
>>> P� Sat, 13 Sep 2008 10:57:36 +0200, skrev William James
>>> <·········@yahoo.com>:
>>>
>>>> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
> [...]
>>>> Ruby:
>>>> IO.readlines( "data2" ).map{|s| s.split }.transpose
>>>>     ==>[["jan", "feb", "mar", "aug", "jun"],
>>>>   ["sun", "mon", "fri", "sat", "tue"],
>>>>   ["12", "14", "23", "3", "15"]]
>>> This sort of fancy hack if fine for mastrubation but useless in real 
>>> code
>>> where ugly details like error handeling get in the way. In this sence 
>>> ruby
>>> is like perl. Favoring quick hacks over robust design. Works for small
>>> problems, a eventual nightmare for large ones.
>>
>> You will always have the mentality of a COBOL
>> programmer, and you will will always be proud
>> of it.
> 
> Well, if the "mentality of a COBOL programmer" means favoring robust 
> design and good debugging practices, then it's nothing to be ashamed of.
> 
> Anyway, I think the biggest problem with the above Ruby code is the use 
> of transpose. Unless Ruby somehow manages to optimize it, it strikes me 
> as a highly inefficient solution.

I think that transpose really doesn�t matter on todays multi-GHz systems.
What William does not realize that his algorithm to solve the task is
pretty much identical to the one that one would use in Lisp.
Some people (I don�t say that William is one of them) think that because
of the existance of some functions in the standard library of a
programming language this specific language is somehow �good�.
In the end it boils down to the algorithms we can write down in our
languages, given the respective library.
Now we might not be able to easily come up with an example from Ruby
which can�t be expressed in pretty much the same complexity in Lisp.
But the other way around I quickly realize that translating
'var
into Ruby is not so obvious. Or the addition of multi methods to Ruby
without actually touching the C code in which Ruby is implemented.
Still, Ruby is a nice language for scripts.


Andr�
-- 
From: Alberto Riva
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <garmuk$ho6m$1@usenet.osg.ufl.edu>
Andr� Thieme wrote:
> Alberto Riva schrieb:
>> William James wrote:
>>> On Sep 13, 6:21 am, "John Thingstad" <·······@online.no> wrote:
>>>> P� Sat, 13 Sep 2008 10:57:36 +0200, skrev William James
>>>> <·········@yahoo.com>:
>>>>
>>>>> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
>> [...]
>>>>> Ruby:
>>>>> IO.readlines( "data2" ).map{|s| s.split }.transpose
>>>>>     ==>[["jan", "feb", "mar", "aug", "jun"],
>>>>>   ["sun", "mon", "fri", "sat", "tue"],
>>>>>   ["12", "14", "23", "3", "15"]]
>>>> This sort of fancy hack if fine for mastrubation but useless in real 
>>>> code
>>>> where ugly details like error handeling get in the way. In this 
>>>> sence ruby
>>>> is like perl. Favoring quick hacks over robust design. Works for small
>>>> problems, a eventual nightmare for large ones.
>>>
>>> You will always have the mentality of a COBOL
>>> programmer, and you will will always be proud
>>> of it.
>>
>> Well, if the "mentality of a COBOL programmer" means favoring robust 
>> design and good debugging practices, then it's nothing to be ashamed of.
>>
>> Anyway, I think the biggest problem with the above Ruby code is the 
>> use of transpose. Unless Ruby somehow manages to optimize it, it 
>> strikes me as a highly inefficient solution.
> 
> I think that transpose really doesn�t matter on todays multi-GHz systems.

I was talking about memory usage, not speed. Building a data structure 
that is not the one you want just to throw it away and rearrange its 
data immediately after may be ok for a toy example like this, but is 
certainly not good if you have matrices with thousands of rows and 
columns.  Unless, as I was saying above, the transpose operator is able 
to transpose a matrix "in place", swapping pointers instead of creating 
a new matrix, but somehow I don't think that's true.

Also, the OP wanted the columns in separate variables. This solution 
still gives you a vector of vectors.

> What William does not realize that his algorithm to solve the task is
> pretty much identical to the one that one would use in Lisp.

Not necessarily. See above.

> Still, Ruby is a nice language for scripts.

That may be true. But exactly for this reason, I would like to see how 
it behaves if you try to transpose a 5000x5000 matrix... :)

Alberto
From: Stanisław Halik
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <gagh5l$185t$1@opal.icpnet.pl>
thus spoke William James <·········@yahoo.com>:

>> CL-USER> (with-open-file (in "/tmp/
>> data.txt")
>>            (loop for line = (read-line in nil
>> nil)
>>                  while line collect (cl-ppcre:split "\\s+" line)))
>> (("jan" "sun" "12")
>>  ("feb" "mon" "14")
>>  ("mar" "fri" "23")
>>  ("aug" "sat"
>> "3")
>>  ("jun" "tue" "15"))

> He wanted a list for each column.

CL-USER> (apply #'mapcar #'list *)
(("jan" "feb" "mar" "aug" "jun")
 ("sun" "mon" "fri" "sat" "tue")
 ("12" "14" "23" "3" "15"))

-- 
The great peril of our existence lies in the fact that our diet consists
entirely of souls. -- Inuit saying
From: André Thieme
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <gaq5ah$c28$1@registered.motzarella.org>
William James schrieb:
> On Sep 12, 3:47 pm, Volkan YAZICI <·············@gmail.com> wrote:
>> On Sep 12, 10:17 pm, Francogrex <······@grex.org> wrote:
>>
>>> Hello, I have a txt file like below: test.txt (only showing a toy
>>> example of 3 cols and 5 rows but there may be 1000s of rows).
>>> jan     sun     12
>>> feb     mon     14
>>> mar     fri     23
>>> aug     sat     3
>>> jun     tue     15
>> READ-LINE an CL-PPCRE are your friends.
>>
>> CL-USER> (with-open-file (in "/tmp/
>> data.txt")
>>            (loop for line = (read-line in nil
>> nil)
>>                  while line collect (cl-ppcre:split "\\s+" line)))
>> (("jan" "sun" "12")
>>  ("feb" "mon" "14")
>>  ("mar" "fri" "23")
>>  ("aug" "sat"
>> "3")
>>  ("jun" "tue" "15"))
> 
> He wanted a list for each column.
> 
> Ruby:
> 
> IO.readlines( "data2" ).map{|s| s.split }.transpose
>     ==>[["jan", "feb", "mar", "aug", "jun"],
>   ["sun", "mon", "fri", "sat", "tue"],
>   ["12", "14", "23", "3", "15"]]

Ruby is prepared for these typical scripting tasks by providing its
user with the regarding functions.
So, a lisper who wants to do scripting would load his scripting lib
and then do:
(transpose (mapcar #'split (readlines "data2")))
==>
(("jan" "feb" "mar" "aug" "jun")
  ("sun" "mon" "fri" "sat" "tue")
  ("12" "14" "23" "3" "15"))

So, it is basically the identical algorithm which works in both
languages. My Lisp example is a bit shorter than yours because I
called my function simply readlines instead of IO.readlines.
But then again I used mapcar instead of map...
Still, Ruby is a nice language for such simple small scripts and
the OP could very well solve this task with a Python, VB or Ruby
one-liner.


-- 
Andr�
From: Pascal J. Bourguignon
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <877i9bb8ul.fsf@hubble.informatimago.com>
Andr� Thieme <······························@justmail.de> writes:
> So, it is basically the identical algorithm which works in both
> languages. My Lisp example is a bit shorter than yours because I
> called my function simply readlines instead of IO.readlines.
> But then again I used mapcar instead of map...
> Still, Ruby is a nice language for such simple small scripts and
> the OP could very well solve this task with a Python, VB or Ruby
> one-liner.

Ruby is nicer than some languages in that it allows parentheses
(practical with paredit):

  (((IO .readlines("/tmp/data2")) .map {|s| (s .split) }) .transpose)

but it is far from being as nice as lisp:

> (transpose (mapcar (function split) (readlines "/tmp/data2")))

Editing the equivalent code is still more work in Ruby, with all sort
of quirks, like you cannot have two expressions on the same line, or
you cannot have spaces between the method name and the parameters, or
some parentheses must be randomly [], {}, or ||, or infix operators,
or the need for random useless keywords, or the fundamental defect
that code has a special syntax which renders almost impossible a good
macro system, and so on.


irb(main):006:0> ([1,2] .map {|x| (puts "hi") (puts "lo") })
SyntaxError: compile error
(irb):6: syntax error, unexpected '(', expecting '}'
([1,2].map{|x| (puts "hi") (puts "lo") })
                            ^
(irb):6: syntax error, unexpected ')', expecting '}'
([1,2].map{|x| (puts "hi") (puts "lo") })
                                      ^
	from (irb):6
	from :0
irb(main):007:0> ([1,2] .map {|x| (puts "hi") 
                    (puts "lo") })
                     hi
lo
hi
lo
[nil, nil]
irb(main):009:0> ([1,2,3] .include? (3 + 2))
(irb):9: warning: don't put space before argument parentheses
false
irb(main):010:0> (def m(x)
                    (if (x == 0)
                       (puts "No way")
                     else                  # !
                       (x - 1)
                     end)                  # !
                   end)                    # !
nil
irb(main):011:0>
                   
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Specifications are for the weak and timid!"