On Saturday, 6 January 2018 at 06:47:33 UTC, Vino wrote:
On Friday, 5 January 2018 at 18:00:34 UTC, thedeemon wrote:
On Friday, 5 January 2018 at 17:59:32 UTC, thedeemon wrote:
Tuple!( staticMap!(Arr, ColumnTypes) ) res; // array of tuples

Sorry, I meant tuple of arrays, of course.

Hi Deemon,

Thank you very much, I tested your code, initially the code did not produce the expected output, and found an issue in the the key line of code as below, after updating the output was as expected. Can you please let me know how to change the array from standard array to container array.

Here's a version with Array, it's very similar:

import std.algorithm: countUntil, joiner, sort, uniq, map;
import std.csv: csvReader;
import std.stdio: File, writeln;
import std.typecons: Tuple, tuple;
import std.meta;
import std.file : readText;
import std.container.array;

alias ColumnTypes = AliasSeq!(string, string, int);

auto readData(string fname) { // returns tuple of Arrays
    Tuple!( staticMap!(Array, ColumnTypes) ) res;
foreach (record; fname.readText.csvReader!(Tuple!ColumnTypes)('\t'))
        foreach(i, T; ColumnTypes)
            res[i].insert(record[i]);
    return res;
}

auto compress(T)(ref Array!T col) {
    auto vals = Array!T( sort(col.dup[]).uniq );
auto ks = Array!ptrdiff_t( col[].map!(v => vals[].countUntil(v)) );
    return tuple(vals, ks);
}

void main() {
    auto columns = readData("data.csv");
    foreach(i, ColT; ColumnTypes) {
        auto vk = compress(columns[i]);
writeln(vk[0][]); //output data, you can write files here
        writeln(vk[1][]); //output indices
    }
}

Reply via email to