Public.Standards.CSV

[ Start > PikeModules > Module > Public.Standards.CSV ] [ Edit this Page | Show Page Versions | Show Formatted ]


This module was made following http://en.wikipedia.org/wiki/Comma-separated_values

The examples shown below use the sample CSV file provided by the fore-mentionned documentation.

1 Files

Public.Standards.CSV.FILE() inherits from the buffered Stdio.FILE() class.

It sports two important methods:
* int write_row(mixed... row)
* int|array read_row()

1.1 Reading

The method-way:

{code}
object infile = Public.Standards.CSV.FILE("/tmp/sample.csv", "r");
infile->read_row();
(1) Result: ({ /* 5 elements */
                "1997",
                "Ford",
                "E350",
                "ac, abs, moon",
                "3000.00"
            })
{code}

The iterator-way:

{code}
object infile = Public.Standards.CSV.FILE("/tmp/sample.csv", "r");
foreach(infile; int line_number; array data) {
//...
}
{code}

1.1 Writing

{code}
object outfile = Public.Standards.CSV.FILE("/tmp/foo.csv", "cw");
outfile->write_row("foo","bar","baz");
(1) Result: 13
{code}

1 String

Public.Standards.CSV.CSVString() deals with string.

As a side note, the name Public.Standards.CSV.String() was originally chosen for consistency, but produced unexpected results i didn't managed to overcome. Anyone with a clue here is welcomed.

Public.Standards.CSV.CSVString() work the very same way than Public.Standards.CSV.FILE()

1.1 Reading

The method way:

{code}
object data = Public.Standards.CSV.CSVString(Stdio.read_bytes("/tmp/sample.csv"));
data->read_row();
(3) Result: ({ /* 5 elements */
                "1997",
                "Ford",
                "E350",
                "ac, abs, moon",
                "3000.00"
            })
{code}

The iterator way:

{code}
object instring = Public.Standards.CSV.CSVString(Stdio.read_bytes("/tmp/sample.csv"));
foreach(instring; int line_number; array dara) { 
// ...
}
{code}

1.1 Writing

Public.Standards.CSV.CSVString() doesn't support writing for now.


Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University