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

Reply via email to