Fastercsv, parse columns into hash/array

I am importing a csv file with the following code. I need to be able to
load the file into a hash or an array so that I can access the values in
each column and row individually. There aore 14 columns in each row and
the number of rows varies from file to file. I have no idea how to do
this as I am a complete noob to the cvs libray and could only find
limited documentation. Any help would be greatly appreciated. TIA

@transactions = []
FasterCSV.foreach("#{RAILS_ROOT}/public/MyExport.csv") do |row|
  @transactions << row
end

SOLVED

@transactions = []
row_number = 0
@balance = 0
FasterCSV.foreach("#{RAILS_ROOT}/public/MyExport.csv") do |row|
  transaction = Transaction.new(:effective_date => row[0],
                               :transaction_type => row[1],
                               :amount => row[2],
                               :description => row[4],
                               :account_type => row[11],
                               :account_number => row[12],
                               :account_alias => row[13])
  if row_number > 1
    @balance += transaction[:amount]
    @transactions << transaction
  end
  row_number += 1
end

biqut2 … wrote:

I am importing a csv file with the following code. I need to be able to
load the file into a hash or an array so that I can access the values in
each column and row individually. There aore 14 columns in each row and
the number of rows varies from file to file. I have no idea how to do
this as I am a complete noob to the cvs libray and could only find
limited documentation. Any help would be greatly appreciated. TIA

@transactions = []
FasterCSV.foreach("#{RAILS_ROOT}/public/MyExport.csv") do |row|
  @transactions << row
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs