Forum: Ruby on Rails Help reading csv data in a dynamic arrays w/ loops... =)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4f127c795ca367f22ec77ac044dac4d?d=identicon&s=25 Jeremy (Guest)
on 2006-02-13 13:51
Hi guys, I'm workin on a little ruby on rails application and I'm having
a little trouble doing something that SHOULD be pretty easy, especially
in rails... So you guys can know what I'm looking for, I included a
block of PHP code that does the job, and the block of ruby code that's
failing me :P. Thanks ahead of time to anyone who can help me out with
this...

- - - - -
<?php
$csv_file_array = functionThatSplitsFileIntoArrayByLine("somefile.csv");

$tcount = 0;
while ( $tcount <= count($csv_file_array) ) {
  $csv_file_array[$tcount] = explode($csv_file_array[$tcount], ",");
  $tcount++;
}
?>
- - - - -

After this php code, $csv_file_array should be a dynamic array where i
can access csv data. ($csv_file_array[1][3] = "First line, and the third
value (start from zero).";)


Now, here is the code im kinda on my way to using with ruby on rails. It
returns an error "index 10 out of array" so the loop's probably going on
longer than it should, i just dont get how rails handles this (no
'var++' incrementing...?)

- - - - -
task_templates = IO.readlines("somefile.csv")

tcount = 0
while tcount <= task_templates.length
  task_templates.fetch(tcount).split(',')
  tcount = tcount.succ
end
- - - - -

Thanks again, hope someone can help me.
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-13 14:34
(Received via mailing list)
Even easier!

require 'csv'

CSV.open("mycsv.csv", "r") do |row|
    value = row[column]
end

Hows that?

-Nick
D4f127c795ca367f22ec77ac044dac4d?d=identicon&s=25 Jeremy (Guest)
on 2006-02-13 19:46
Great, thanks alot nick!
This topic is locked and can not be replied to.