trouble with associative Arrays

atzensepp webwicht at web.de
Sat Jan 20 14:33:24 UTC 2024


Hello,

I am new with D and want to convert a c program for a csv file 
manipulation with exhaustive dynamic memory mechanics to D .

When reading a CSV-file line by line I would like to create an 
associative array to get the row values by the value in the 
second column.
Although I save the rows in an array (to get different pointers 
to the values) the program below always gives the last row.
I am sure someone could help.

     thanks


     void main( string args[])
     {
         auto file = File("transpatch2_orig.csv"); // Open for 
reading
         auto range = file.byLine();
         // Print first three lines
         foreach (line; range.take(1))
             writeln(line);
         auto i=0;

         char [][] [string] orgids;
         char [][][] rows;
         foreach (line; range)
         {
             if (!line.empty)
             {
                // auto row = line.split(";");
                 rows ~= (line.split(";"));
                 string word = rows[$ - 1][1].idup;
                 if(word.length>0 && word[0] == '\"')
                         word= word[1 .. $-1];
                 orgids[word.idup]=rows[$ - 1];
                 i++;
             }
         }

         writeln( orgids.length);
         writeln( args[1],orgids[args[1]]);
         writeln( args[2],orgids[args[2]]);
         writeln("Lines: ",i);
     }



More information about the Digitalmars-d-learn mailing list