2.4 Mtable - Read CSV data into cell class

This class read specified CSV data into random access memory by cell. Its features are as follows.

2.4.1 Method

* MCMD::Mtable::new(arguments)

Generate Mtable object. At the arguments parameter, specify the following arguments in character string separated with a space.

i=

Input file name (String)

-nfn

No field name in the first row.

* MCMD::Mtable::cell(col=0, row=0) -> String

Return the value of cell to the corresponding row and col (column). row and col is assigned by sequential row and column number. Both row and column number are expressed in integer starting from 0 (Column number of Mcsvin starts from 1). Returns nil if row and col are out of range.

row

Row number. Positive integer value including 0. Default value is 0.

col

Column number. Positive integer value including 0. Field name cannot be specified. Default value is 0.

Returns the value of col number of the column as col. It is equivalent to specifying cell(col, 0). However, if both col and row is not specified, it returns 0th item of 0th row. It is equivalent to specifying cell(0,0).

* MCMD::Mtable::names() -> String Array

Return the field name array.

* MCMD::Mtable::name2num() -> String=>Fixnum Hash

Return Hash of key of field name and the the corresponding field number.

* MCMD::Mtable::size() -> Fixnum

Return the number of rows.

2.4.2 Examples

Example 1

# dat1.csv
customer,date,amount
A,20081201,10
B,20081002,40

tbl=MCMD::Mtable.new("i=dat1.csv")
p tbl.names     # -> ["customer", "date", "amount"]
p tbl.name2num  # -> {"amount"=>2, "date"=>1, "customer"=>0}
p tbl.size      # -> 2
p tbl.cell(0,0) # -> "A"
p tbl.cell(0,1) # -> "B"
p tbl.cell(1,1) # -> "20081202"
p tbl.cell(1)   # -> "20081201"
p tbl.cell      # -> "A"

Example 2 Without column names in the first row

# dat1.csv
customer,date,amount
A,20081201,10
B,20081002,40

tbl=MCMD::Mtable.new("i=dat1.csv -nfn") # Treats the first row as data.
p tbl.names     # -> nil
p tbl.name2num  # -> nil
p tbl.size      # -> 3
p tbl.cell(0,0) # -> "customer"
p tbl.cell(0,1) # -> "A"
p tbl.cell(1,1) # -> "20081201"
p tbl.cell(1)   # -> "date"
p tbl.cell      # -> "customer"

2.4.3 Related Command

Mcsvin : Read CSV data.