Row
Row operations consist of the
filters,
transformers, and
bufferings. Each of these
are described in
Basics and
Reference documentation.
Pnyx will automatically
Shim Line operations (filters and transformers) whenever
calling
Line methods while in
Row state. In addition to these operations, the following operations are available:
The rest of this page illustrates some usages these operations.
Shim Examples
By default, when shimming
Line filters, a boolean
OR operation is performed. This means that a
Row will be
matched, if
any column matches the
ILineFilter. To match
each column, use the
shimAnd method. An example
of both are shown below.
Shim OR:
const String input = @"Line one,a,,c
Line two,a,b,c
Line three,,,c
";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.hasLine();
p.writeStdout();
}
// outputs:
// "Line one",a,,c
// "Line two",a,b,c
// "Line three",,,c
Shim AND:
const String input = @"Line one,a,,c
Line two,a,b,c
Line three,,,c
";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.shimAnd(p2 => p2.hasLine());
p.writeStdout();
}
// outputs:
// "Line two",a,b,c
Custom Operations
The built-in
Row opertions for
Pnyx only cover the very basics. For any advanced application, custom
operations will be used extensively. In this case, each
Row interface is simple to implement and easily tested
with Unit Tests. In addition to implementing interfaces,
Pnyx supports using Lambda expression for
quicker, more flexible usage. The following examples shows the two
Row operations with Lambda expressions.
rowFilter (lambda):
const String input = @"Line one,KEEPER
Line two,Loser
";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.rowFilter(x => x[1].isUpperCase());
p.writeStdout();
}
// outputs:
// "Line one",KEEPER
rowTransformer (lambda):
const String input = @"Line one,KEEPER
Line two,LOSER
";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.rowTransformer(x => new List<string?> {x[1].toTitleCase()});
p.writeStdout();
}
// outputs:
// Keeper
// Loser
Convert To Line
To convert
Row data to
Line data, there are several methods available. These methods:
print,
printDelimiter, and
printTab are
documented in
Reference, Conversion. For advanced formatting,
IRowConverter
should be implemented and used with
rowToLine method. Below are some examples of converting
Row data to
Line data.
print
const String input = "Socialism,Communism,Fascism";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.print("Failed Systems: $1, $2, and $3");
p.writeStdout();
}
// outputs: Failed Systems: Socialism, Communism, and Fascism
printDelimiter
const String input = "col1,\"Column, zwei\"";
await using (Pnyx p = new Pnyx())
{
p.readString(input);
p.parseCsv();
p.printDelimiter("|");
p.writeStdout();
}
// outputs: col1|Column, zwei
Column Operation
IRowFilters and
IRowTransformers are designed to work with all columns. When
shimming
ILineFilters or
ILineTransformers, every column is affected. In many cases, you may want to operate on
only one column. For this, use
rowFilterColumn and
rowTransformerColumn methods. These methods
take a column index (zero-based) and a line filter or transformer. An example
of both are shown below.
rowFilterColumn
const String input = @"
Line one,Atlanta,City located in the southwestern United States.
Line two,New York,The most populous city in the United States.
Line three,Tampa,Sunny and warm city on the west coast of Florida.
";
await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv();
p.rowFilterColumn(RowConstants.B, new Grep("a"));
p.writeStdout();
// outputs:
// Line one,Atlanta,City located in the southwestern United States.
// Line three,Tampa,Sunny and warm city on the west coast of Florida.
rowTransformerColumn
const String input = @"
Line one,Atlanta
Line two,New York
Line three,Tampa
";
await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv();
p.rowTransformerColumn(RowConstants.B, new SedReplace(".*", @"City of \0"));
p.writeStdout();
// outputs:
// Line one,City of Atlanta
// Line two,City of New York
// Line three,City of Tampa
Columns Operation
IRowFilters and
IRowTransformers are designed to work with all columns. To apply to one column, see
Column Operation. To apply to multiple columns, use
withColumns method. This works
by wrapping a lamba expression. Every row operation performed within the lamba is restricted to operate on the
specified column(s). Here is an example of filters and transforms that are only applied to a column.
withColumns (filter)
const String input = @"
Line one,Atlanta,City located in the southwestern United States.
Line two,New York,The most populous city in the United States.
Line three,Tampa,Sunny and warm city on the west coast of Florida.
";
await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv();
p.withColumns(p2 => p2.grep("a"), RowConstants.B);
p.writeStdout();
// outputs:
// Line one,Atlanta,City located in the southwestern United States.
// Line three,Tampa,Sunny and warm city on the west coast of Florida.
withColumns (transform)
const String input = @"
Line one,Atlanta
Line two,New York
Line three,Tampa
";
await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv();
p.withColumns(p2 => p2.sed(".*", @"City of \0"), RowConstants.B);
p.writeStdout();
// outputs:
// Line one,City of Atlanta
// Line two,City of New York
// Line three,City of Tampa
Next
Suggested next steps:
- Line, learn more about Line operations
- Object, examples of Object operations
- NameValuePair, examples of Name-Value Pair operations
- Input, learn more about Input operations
- Output, learn more about Output operations