www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - naming a variable at runtime

reply "InfinityPlusB" <brian infinityplusb.com> writes:
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");
}
May 12 2014
next sibling parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
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
parent "InfinityPlusB" <brian infinityplusb.com> writes:
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
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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.html
 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");
 }
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
parent reply "InfinityPlusB" <brian infinityplusb.com> writes:
On Tuesday, 13 May 2014 at 04:26:04 UTC, Ali Çehreli wrote:
 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.
That's good to know, I'll stop trying to make that happen. :P
 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
yup, that will work. If I wasn't hell bent on naming variables, I probably would have figured this out. :P Thanks.
May 12 2014
parent Jacob Carlborg <doob me.com> writes:
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. :P
Perhaps you could use an associative array. Then you get sort of named variables. -- /Jacob Carlborg
May 12 2014