Jump to page: 1 2 3
Thread overview
Error: variable i cannot be read at compile time
Jan 04, 2018
Vino
Jan 04, 2018
Ali Çehreli
Jan 05, 2018
Vino
Jan 05, 2018
thedeemon
Jan 05, 2018
Vino
Jan 05, 2018
thedeemon
Jan 05, 2018
Vino
Jan 05, 2018
Vino
Jan 05, 2018
thedeemon
Jan 05, 2018
thedeemon
Jan 05, 2018
thedeemon
Jan 06, 2018
Vino
Jan 06, 2018
Vino
Jan 06, 2018
thedeemon
Jan 07, 2018
Vino
Jan 07, 2018
Vino
Jan 07, 2018
thedeemon
Jan 07, 2018
Vino
Jan 08, 2018
thedeemon
Jan 08, 2018
Vino
Jan 08, 2018
thedeemon
Jan 08, 2018
thedeemon
Jan 08, 2018
Vino
January 04, 2018
Hi All,

 Request your help on the below error for the below program.

Error:
CReadCol.d(20): Error: variable i cannot be read at compile time
CReadCol.d(21): Error: variable i cannot be read at compile time
CReadCol.d(22): Error: variable i cannot be read at compile time


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

auto read () {
Array!string Ucol1, Ucol2;
Array!int Ucol3;
int rSize;
auto file = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\ColRead.csv", "r");
foreach (record; file.byLineCopy.joiner("\n").csvReader!(Tuple!(string, string, int)))
{ Ucol1.insertBack(record[0]); Ucol2.insertBack(record[1]); Ucol3.insertBack(record[2]); rSize = record.length; }
return tuple(Ucol1, Ucol2, Ucol3, rSize);
}
/***********************************************************************************/
auto Master (int Size) {
Array!int Keycol;
for(int i = 0; i < Size; i++) {
typeof(read()[i]) Datacol;
Datacol.insertBack(sort(read[i].dup[]).uniq);
foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k)); } }
return tuple (Datacol[], Keycol[]);
}

void main () {
int Size = read[3];
writeln(Master(Size));
}

From,
Vino.B
January 04, 2018
On 01/04/2018 08:51 AM, Vino wrote:

> auto read () {
[...]
> return tuple(Ucol1, Ucol2, Ucol3, rSize);
> }

read() returns a tuple of values of different types.

> for(int i = 0; i < Size; i++) {
> typeof(read()[i]) Datacol;

typeof is a compile-time expression but there cannot be a consistent result to that expression when i is not known at compile-time.

You might try using a 'static foreach' but this time Size is not a compile-time expression:

    static foreach(i; 0 .. Size) {
        typeof(read()[i]) Datacol;

Error: variable Size cannot be read at compile time

Ali

January 05, 2018
On Thursday, 4 January 2018 at 18:49:21 UTC, Ali Çehreli wrote:
> On 01/04/2018 08:51 AM, Vino wrote:
>
> > auto read () {
> [...]
> > return tuple(Ucol1, Ucol2, Ucol3, rSize);
> > }
>
> read() returns a tuple of values of different types.
>
> > for(int i = 0; i < Size; i++) {
> > typeof(read()[i]) Datacol;
>
> typeof is a compile-time expression but there cannot be a consistent result to that expression when i is not known at compile-time.
>
> You might try using a 'static foreach' but this time Size is not a compile-time expression:
>
>     static foreach(i; 0 .. Size) {
>         typeof(read()[i]) Datacol;
>
> Error: variable Size cannot be read at compile time
>
> Ali

Hi Ali,

  Thank you very much, can you suggest the best way around this issue.

From,
Vino.B
January 05, 2018
On Friday, 5 January 2018 at 09:09:00 UTC, Vino wrote:
>   Thank you very much, can you suggest the best way around this issue.

What exactly are you trying to do in Master()? The code seems very broken. Each time you write read[i] is will call read() and read the whole file, you're going to read the file so many times in this code. I don't think that was the intent.

January 05, 2018
On Friday, 5 January 2018 at 12:10:33 UTC, thedeemon wrote:
> On Friday, 5 January 2018 at 09:09:00 UTC, Vino wrote:
>>   Thank you very much, can you suggest the best way around this issue.
>
> What exactly are you trying to do in Master()? The code seems very broken. Each time you write read[i] is will call read() and read the whole file, you're going to read the file so many times in this code. I don't think that was the intent.

Hi,

  Please find the full code, the below code will read a ColRead.csv file which contains the below entry

Miller	America	23
John	India	42
Baker	Austrilia	21
Zsuwalski	Japan	45
Baker	America	45
Miller	India	23


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

auto read (){
Array!string Ucol1, Ucol2; Array!int Ucol3; int rSize;
auto file = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\ColRead.csv", "r");
foreach (record; file.byLineCopy.joiner("\n").csvReader!(Tuple!(string, string, int)))
{ Ucol1.insertBack(record[0]); Ucol2.insertBack(record[1]); Ucol3.insertBack(record[2]); rSize = record.length; }
return tuple(Ucol1, Ucol2, Ucol3, rSize);
}

void main () {
Array!int Key;
int Size = read[3];
static foreach(i; 0 .. Size) {
typeof(read()[i]) Data;
Data.insertBack(sort(read[0].dup[]).uniq);
foreach(i; read[i]) { Key.insertBack(Data[].countUntil(i)); } }
 }

January 05, 2018
On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote:
>> What exactly are you trying to do in Master()?
>   Please find the full code,

Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written.

January 05, 2018
On Friday, 5 January 2018 at 12:47:39 UTC, thedeemon wrote:
> On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote:
>>> What exactly are you trying to do in Master()?
>>   Please find the full code,
>
> Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written.

Hi,

I am trying to implement data dictionary compression, and below is the function of the program,

Function read:
This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data.

CSV file content:
Miller	America	23
John	India	42
Baker	Australia	21
Zsuwalski	Japan	45
Baker	America	45
Miller	India	23

Function Main
This function receives the data from the function read.
Creates an array based of the function return type – ( typeof(read()[i]) Data );
Sorts the data and removes the duplicates and stores the data in the above array.
Then using “countUntil” function we can accomplish the data dictionary compression.

Result
The above file will be stored as
Data File:
Data-Col1.txt which contains [Baker, John, Miller, Zsuwalski]
Data-Col2.txt which contains [America, Australia , India, Japan]
Data-Col3.txt which contains [21, 23, 42, 45]

Index File:
Index-Col1.txt which contains [2, 1, 0, 3, 0, 2]
Index -Col2.txt which contains [0, 2, 1, 3, 0, 2]
Index -Col3.txt which contains [1, 2, 0, 3, 3, 1]

The program works for a single column.

From,
Vino.B

January 05, 2018
On Friday, 5 January 2018 at 13:09:25 UTC, Vino wrote:
> On Friday, 5 January 2018 at 12:47:39 UTC, thedeemon wrote:
>> On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote:
>>>> What exactly are you trying to do in Master()?
>>>   Please find the full code,
>>
>> Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written.
>
> Hi,
>
> I am trying to implement data dictionary compression, and below is the function of the program,
>
> Function read:
> This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data.
>
> CSV file content:
> Miller	America	23
> John	India	42
> Baker	Australia	21
> Zsuwalski	Japan	45
> Baker	America	45
> Miller	India	23
>
> Function Main
> This function receives the data from the function read.
> Creates an array based of the function return type – ( typeof(read()[i]) Data );
> Sorts the data and removes the duplicates and stores the data in the above array.
> Then using “countUntil” function we can accomplish the data dictionary compression.
>
> Result
> The above file will be stored as
> Data File:
> Data-Col1.txt which contains [Baker, John, Miller, Zsuwalski]
> Data-Col2.txt which contains [America, Australia , India, Japan]
> Data-Col3.txt which contains [21, 23, 42, 45]
>
> Index File:
> Index-Col1.txt which contains [2, 1, 0, 3, 0, 2]
> Index -Col2.txt which contains [0, 2, 1, 3, 0, 2]
> Index -Col3.txt which contains [1, 2, 0, 3, 3, 1]
>
> The program works for a single column.
>
> From,
> Vino.B

More Info:

If we change the below line
static foreach(i; 0 .. 1)
Output: ["Baker", "John", "Miller", "Zsuwalski"][2, 1, 0, 3, 0, 2]

static foreach(i; 1 .. 2)
["America", "Austrilia", "India", "Japan"][0, 2, 1, 3, 0, 2])

static foreach(i; 2 .. 3)
[21, 23, 42, 45][1, 2, 0, 3, 3, 1]

Instead of manually chaning the values I used the variable Size where the value of the Size if from the read function (read[3] ) where read[3] is rSize = record.length;

If I use the variable Size as static foreach(i; 0 .. Size) I am getting an error : “Error: variable Size cannot be read at compile time”.

From,
Vino.B

January 05, 2018
On Friday, 5 January 2018 at 13:09:25 UTC, Vino wrote:
>> Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written.
>
> Hi,
>
> I am trying to implement data dictionary compression, and below is the function of the program,
>
> Function read:
> This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data.
>
> CSV file content:
> Miller	America	23
> John	India	42
> Baker	Australia	21
> Zsuwalski	Japan	45
> Baker	America	45
> Miller	India	23
>
> Function Main
> This function receives the data from the function read.
> Creates an array based of the function return type – ( typeof(read()[i]) Data );
> Sorts the data and removes the duplicates and stores the data in the above array.
> Then using “countUntil” function we can accomplish the data dictionary compression.

Thank you for the explanation, this is a nice little task.
Here's my version of solution. I've used ordinary arrays instead of std.container.array, since the data itself is in GC'ed heap anyway.
I used csv file separated by tabs, so told csvReader to use '\t' for delimiter.

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.array : array;

//we know types of columns, so let's state them once
alias ColumnTypes = AliasSeq!(string, string, int);
alias Arr(T) = T[];

auto readData() {
    auto file = File("data.csv", "r");
    Tuple!( staticMap!(Arr, ColumnTypes) ) res; // tuple of arrays
    foreach (record; file.byLineCopy.joiner("\n").csvReader!(Tuple!ColumnTypes)('\t'))
        foreach(i, T; ColumnTypes)
            res[i] ~= record[i]; // here res[i] can have different types
    return res;
}

//compress a single column
auto compress(T)(T[] col) {
    T[] vals = sort(col.dup[]).uniq.array;
    auto ks = col.map!(v => col.countUntil(v)).array;
    return tuple(vals, ks);
}

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

January 05, 2018
On Friday, 5 January 2018 at 17:50:13 UTC, thedeemon wrote:
> Here's my version of solution. I've used ordinary arrays instead of std.container.array, since the data itself is in GC'ed heap anyway.
> I used csv file separated by tabs, so told csvReader to use '\t' for delimiter.

And since lines of the file are copied to heap anyway, it's easier to skip unnecessary line splitting and joining and do the reading simpler:

import std.file : readText;

auto readData(string fname) {
    Tuple!( staticMap!(Arr, ColumnTypes) ) res; // array of tuples
    foreach (record; fname.readText.csvReader!(Tuple!ColumnTypes)('\t'))
        foreach(i, T; ColumnTypes)
            res[i] ~= record[i];
    return res;
}

« First   ‹ Prev
1 2 3