naming a variable at runtime
InfinityPlusB via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 12 20:47:36 PDT 2014
Hi clever people
I'm trying to do something which I thought would be easy.
Read a file in, and for every row, create a array.
I want to be able to name the rows, as they are built.
So when row 1 is read in I get
int[] bob_1 = new int[0];
when the second row is read in, I get
int[] bob_2 = new int[0];
So at the end of running my program I effectively want bob_1,
bob_2 and bob_3.
And then I can do something more interesting with them ...
I realise this is now slightly beyond my if-then-else
capabilities, and was wondering if I could get some direction.
Thanks
B
The contents of /home/bob/test.csv
-1, -1, 1, -1, -1
-1, 1, 1, 1, -1
1, -1, -1, 1, -1
My Program
#!/usr/bin/rdmd
import std.stdio;
import std.array;
import std.conv;
import std.string;
void main()
{
string inputFile = "/home/bob/test.csv";
// string inputFile = "-1, -1, 1, -1, -1\n-1, 1, 1, 1, -1\n1,
-1, -1, 1, -1\r\n";
auto readInFile = File(inputFile);
int count = 0;
foreach(line; readInFile.byLine())
{
int[] bob = new int[0];
// int[] bob_NUMBER_ME = new int[0];
foreach(item;line.split(","))
{
writeln(strip(item));
bob ~= to!int(strip(item));
}
writeln(bob);
writefln("Line number %d", count);
count++;
}
writeln("Done");
}
More information about the Digitalmars-d-learn
mailing list