This class read specified CSV data into random access memory by cell. Its features are as follows.
Access cells in random order by the specified row and column.
Data is read-only and cannot insert or update records.
All data is read as character string. Corresponding type conversion (ex. to_i) is necessary for other usage.
Read data into available memory. The program terminates with an error when there is insufficient space.
* 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 number. Positive integer value including 0. Default value is 0.
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.
# 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"
# 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"
Mcsvin : Read CSV data.