On Nov 20, 2005, at 7:37 PM, anne001 wrote:

> I would like to parse some excel CVS data which has a repetitive block
> pattern
>
> "Experiment ID:	1",,,,,,,,,,,,
> "Subject ID:	1013938829432171e868c340.
> Trial,stimulus,time,type,field1,field2,text_response,Abs. time of
> response,,,,,
> 26,undefined,14828,KEY,RETURN,UNUSED,DCS,Sat Oct 15 17:48:04 GMT-0400
> 2005,,,,,
> 23,undefined,15078,KEY,RETURN,UNUSED,244,Sat Oct 15 17:48:19 GMT-0400
> 2005,,,,,
> 7,nixontrialleft copy.pct [TAG: 1],5953,KEY,1,UNUSED,,Sat Oct 15
> 17:49:24 GMT-0400 2005,,,,,
> 8,nixontrialfront copy.pct [TAG: 3],6250,KEY,3,UNUSED,,Sat Oct 15
> 17:49:31 GMT-0400 2005,,,,,
> 9,nixontrialright copy.pct [TAG: 2],2469,KEY,2,UNUSED,,Sat Oct 15
> 17:49:34 GMT-0400 2005,,,,,
> #####
> more data
> ######
> ,,,,,,,,,,4374.347222,,
> ,,,,,,,,,,,,1.00
> ,,,,,,,,,,,,0.93
> ### and a new block starts
> "Experiment ID:	3",,,,,,,,,,,,0.92
> ....
>
> Question 1:
> ------------------
> Arr = IO.readlines(File.expand_path("~/Desktop/FaceRetest.cvs"))
> has a length of 1, why?

Well, if you're on Windows, Ruby is looking for a \015\012 sequence  
to end the line.  On Unix it would be looking for a \012...

> I noticed that the puts has ^M everywhere

^M (control-M) is just \015. So it's not seeing the line endings.   
Some OSes actually used this line ending, like Mac OS 9 and lower,  
but it's pretty rare.

> Arr =
> IO.readlines(File.expand_path("~/Desktop/ 
> FaceRetest.cvs"),sep_string="^M")

That's a fine fix.  You should be able to replace sep_string="^M"  
with just "\r", I think.

> seems to split the array into lines.
> Arr = Arr.split(",")
> gives me a message, private method `split' called

First, don't start Ruby variable names with a capital letter.  This  
isn't your problem here, but it's still not a habit to get into.  A  
capital variable is a constant in Ruby.

The real problem here is that Arr is an Array, and you are calling a  
String function on it, split().  Try:

Arr.first.split(",")

# ... or ...

Arr.map { |row| row.split(",") }

But it's better to use a real parser as Ara suggested.

> puts Arr.length gets me no response. I expected split to take the 1D
> array and transform it into a 2D array.
>
> What is the best way to get the excel CVS data into a Ruby 2D array  
> for
> further analysis?

Try:

require "csv"
arr = CSV.read(File.expand_path("~/Desktop/FaceRetest.cvs"))

It gives two-dimensional arrays:

Neo:~/Desktop$ cat data.csv
1,2,3
4,,5
Neo:~/Desktop$ ruby -r pp -r csv -e 'pp CSV.read("data.csv")'
[["1", "2", "3"], ["4", nil, "5"]]

> Question 2:
> -----------------
> Once I have a 2D array, what is the best way to find the index of the
> key word Trial (which starts the data I want).

Hmm, what about something like:

in_section = false

csv.each do |row|
   if row.first == "Trial"
     in_section = true
     next
   elsif in_section
     # process row here...
   end
end

Hope that gets you going.

James Edward Gray II