digitalmars.D.learn - naming a variable at runtime
- InfinityPlusB (47/47) May 12 2014 Hi clever people
- safety0ff (12/12) May 12 2014 You should look into associative arrays (
- InfinityPlusB (12/24) May 12 2014 Thanks for the quick reply, I'll look into that.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (24/66) May 12 2014 First, no, you cannot name variables at run time because variables are
- InfinityPlusB (6/16) May 12 2014 yup, that will work.
- Jacob Carlborg (5/8) May 12 2014 Perhaps you could use an associative array. Then you get sort of named
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
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");
}
May 12 2014
You should look into associative arrays (
http://dlang.org/hash-map .)
Example:
import std.stdio;
void main()
{
int[][string] mybobs;
mybobs["bob_1"] = [-1, -1, 1, -1, -1];
mybobs["bob_2"] = [-1, 1, 1, 1, -1];
mybobs["bob_3"] = [-1, 1, 1, 1, -1];
writeln(mybobs);
}
May 12 2014
On Tuesday, 13 May 2014 at 03:54:33 UTC, safety0ff wrote:
You should look into associative arrays (
http://dlang.org/hash-map .)
Example:
import std.stdio;
void main()
{
int[][string] mybobs;
mybobs["bob_1"] = [-1, -1, 1, -1, -1];
mybobs["bob_2"] = [-1, 1, 1, 1, -1];
mybobs["bob_3"] = [-1, 1, 1, 1, -1];
writeln(mybobs);
}
Thanks for the quick reply, I'll look into that.
My issue is (which I didn't explain clearly, sorry) how do I do
it with an unknown number of lines?
So my example has 3 lines. What if it had 300? or 3000?
Basically so I can run the same piece of code for any size file,
and it will create a new named array for each line.
So I assume(?) I have to do something that will "name" each of
these arrays at runtime.
So,
for(x;1 ... n)
bob_x = ...
May 12 2014
On 05/12/2014 08:47 PM, InfinityPlusB wrote:I want to be able to name the rows, as they are built.First, no, you cannot name variables at run time because variables are concepts of source code; they don't exist in the compiled program.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];Well, it looks like a bob array. :) How about "naming" those rows as bob[0], bob[1], etc.So at the end of running my program I effectively want bob_1, bob_2 and bob_3.Would zero-indexing work?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.I had used the same naming scheme as a segway to my arrays chapter: http://ddili.org/ders/d.en/arrays.htmlThe contents of /home/bob/test.csv -1, -1, 1, -1, -1 -1, 1, 1, 1, -1 1, -1, -1, 1, -1 My Program 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"); }Here is the inner loop with minimal changes to your program: int[][] bob; // <== Array of arrays foreach(line; readInFile.byLine()) { int[] row; // <== Make a new row foreach(item;line.split(",")) { writeln(strip(item)); row ~= to!int(strip(item)); } bob ~= row; // <== Add the row writefln("Line number %d", count); count++; } writeln(bob); Ali
May 12 2014
On Tuesday, 13 May 2014 at 04:26:04 UTC, Ali Çehreli wrote:On 05/12/2014 08:47 PM, InfinityPlusB wrote:That's good to know, I'll stop trying to make that happen. :PI want to be able to name the rows, as they are built.First, no, you cannot name variables at run time because variables are concepts of source code; they don't exist in the compiled program.Here is the inner loop with minimal changes to your program: int[][] bob; // <== Array of arrays foreach(line; readInFile.byLine()) { int[] row; // <== Make a new rowyup, that will work. If I wasn't hell bent on naming variables, I probably would have figured this out. :P Thanks.
May 12 2014
On 13/05/14 06:32, InfinityPlusB wrote:yup, that will work. If I wasn't hell bent on naming variables, I probably would have figured this out. :PPerhaps you could use an associative array. Then you get sort of named variables. -- /Jacob Carlborg
May 12 2014









"InfinityPlusB" <brian infinityplusb.com> 