Jeremy
2006-Feb-13 12:52 UTC
[Rails] Help reading csv data in a dynamic arrays w/ loops... =)
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. -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Feb-13 13:34 UTC
[Rails] Help reading csv data in a dynamic arrays w/ loops... =)
Even easier! require ''csv'' CSV.open("mycsv.csv", "r") do |row| value = row[column] end Hows that? -Nick On 2/13/06, Jeremy <jcsarda@gmail.com> wrote:> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >