# Fastercsv, parse columns into hash/array

**URL:** <https://www.ruby-forum.com/t/fastercsv-parse-columns-into-hash-array/144275>\
**Category:** Rails\
**Created:** [July 28, 2008, 3:11am UTC](https://www.ruby-forum.com/t/fastercsv-parse-columns-into-hash-array/144275 "2008-07-28T03:11:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![biqut2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@biqut2](https://www.ruby-forum.com/u/biqut2)\
**Post date:** [July 28, 2008, 3:11am UTC](https://www.ruby-forum.com/t/fastercsv-parse-columns-into-hash-array/144275/1 "2008-07-28T03:11:56Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![biqut2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@biqut2](https://www.ruby-forum.com/u/biqut2)\
**Post date:** [July 28, 2008, 6:46am UTC](https://www.ruby-forum.com/t/fastercsv-parse-columns-into-hash-array/144275/2 "2008-07-28T06:46:14Z")

</div>

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
> ```
