Sha256: 82e20d7d52d98af2b2dd1e01dd3188fc421faccf5d9a3c3a7765bc7e7f6bd378

Contents?: true

Size: 1.16 KB

Versions: 19

Compression:

Stored size: 1.16 KB

Contents

#!/usr/local/bin/ruby -w

# csv_table.rb
#
#  Created by James Edward Gray II on 2006-11-04.
#  Copyright 2006 Gray Productions. All rights reserved.
# 
# Feature implementation and example code by Ara.T.Howard.

require "faster_csv"

table = FCSV.parse(DATA, :headers => true, :header_converters => :symbol)

# row access
table[0].class   # => FasterCSV::Row
table[0].fields  # => ["zaphod", "beeblebrox", "42"]

# column access
table[:first_name]  # => ["zaphod", "ara"]

# cell access
table[1][0]            # => "ara"
table[1][:first_name]  # => "ara"
table[:first_name][1]  # => "ara"

# manipulation
table << %w[james gray 30]
table[-1].fields  # => ["james", "gray", "30"]

table[:type] = "name"
table[:type]  # => ["name", "name", "name"]

table[:ssn] = %w[123-456-7890 098-765-4321]
table[:ssn]  # => ["123-456-7890", "098-765-4321", nil]

# iteration
table.each do |row|
  # ...
end

table.by_col!
table.each do |col_name, col_values|
  # ...
end

# output
puts table
# >> first_name,last_name,age,type,ssn
# >> zaphod,beeblebrox,42,name,123-456-7890
# >> ara,howard,34,name,098-765-4321
# >> james,gray,30,name,

__END__
first_name,last_name,age
zaphod,beeblebrox,42
ara,howard,34

Version data entries

19 entries across 19 versions & 6 rubygems

Version Path
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/fastercsv-1.5.5/examples/csv_table.rb
StephanZ-fastercsv-1.4.1 examples/csv_table.rb
b2b2dot0-fastercsv-1.4.1 examples/csv_table.rb
fastercsv-1.5.5 examples/csv_table.rb
glennfu-faster_csv-1.5.5.1 examples/csv_table.rb
fastercsv-1.5.4 examples/csv_table.rb
rw_fastercsv-1.5.7 examples/csv_table.rb
rw_fastercsv-1.5.6 examples/csv_table.rb
fastercsv-1.5.3 examples/csv_table.rb
fastercsv-1.5.1 examples/csv_table.rb
fastercsv-1.1.0 examples/csv_table.rb
fastercsv-1.2.0 examples/csv_table.rb
fastercsv-1.0.0 examples/csv_table.rb
fastercsv-1.1.1 examples/csv_table.rb
fastercsv-1.2.2 examples/csv_table.rb
fastercsv-1.5.0 examples/csv_table.rb
fastercsv-1.2.3 examples/csv_table.rb
fastercsv-1.4.0 examples/csv_table.rb
fastercsv-1.2.1 examples/csv_table.rb