Multi dimensional array format priting

Vino.B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 25 11:52:30 PDT 2017


On Friday, 25 August 2017 at 17:41:31 UTC, Vino.B wrote:
> On Friday, 25 August 2017 at 17:02:53 UTC, Jonathan M Davis 
> wrote:
>> On Friday, August 25, 2017 16:45:16 Vino.B via 
>> Digitalmars-d-learn wrote:
>>> Hi,
>>>
>>>   Request your help on the below issue,
>>>
>>> Issue : While appending data to a array the data is getting 
>>> duplicated.
>>>
>>> Program:
>>> import std.file: dirEntries, isFile, SpanMode;
>>> import std.stdio: writeln, writefln;
>>> import std.algorithm: filter, map;
>>> import std.array: array;
>>> import std.typecons: tuple;
>>>
>>> string[] Subdata;
>>> void main ()
>>> {
>>>   auto dFiles = dirEntries("C:\\Temp\\TEAM",
>>> SpanMode.shallow).filter!(a => a.isFile).map!(a => 
>>> tuple(a.name ,
>>> a.timeCreated)).array;
>>>   foreach (d; dFiles)
>>>        {
>>>         Subdata ~= d[0];
>>>         Subdata ~= d[1].toSimpleString;
>>>         writeln(Subdata);
>>>         }
>>> }
>>>
>>> Output:
>>>
>>> ["C:\\Temp\\TEAM\\test1.pdf", "2017-Aug-24 18:23:00.8946851"]
>>>  -
>>> duplicate line
>>> ["C:\\Temp\\TEAM\\test1.pdf", "2017-Aug-24 18:23:00.8946851",
>>> "C:\\Temp\\\\TEAM\\test5.xlsx", "2017-Aug-25 23:38:14.486421"]
>>
>> You keep printing out the entire array on every iteration of 
>> the loop, so of course, you're going to see stuff output 
>> multiple times. If you did something like
>>
>>     import std.stdio;
>>
>>     void main()
>>     {
>>         int[] arr;
>>         foreach(i; 0 .. 5)
>>         {
>>             arr ~= i * 10;
>>             writeln(arr);
>>         }
>>     }
>>
>> then you'd get the output
>>
>> [0]
>> [0, 10]
>> [0, 10, 20]
>> [0, 10, 20, 30]
>> [0, 10, 20, 30, 40]
>>
>> whereas if you did
>>
>>     import std.stdio;
>>
>>     void main()
>>     {
>>         int[] arr;
>>         foreach(i; 0 .. 5)
>>             arr ~= i * 10;
>>         writeln(arr);
>>     }
>>
>> then you'd just get
>>
>> [0, 10, 20, 30, 40]
>>
>> - Jonathan M Davis
>
> Hi All,
>
>  Thank you very much, that was my mistake. The main idea is to 
> implement parallelism and now i get only single data as there 
> are 2 files in each of the folders, but it is listing only 1 
> per folder.
>
> import std.file: dirEntries, isFile, SpanMode;
> import std.stdio: writeln, writefln;
> import std.algorithm: filter, map;
> import std.array: array;
> import std.typecons: tuple;
> import std.parallelism;
> string[] Subdata;
>
> auto Dirlst = [ "C:\\Temp\\TEAM", "C:\\Temp\\PROD_TEAM"];
>
> string[] Test (string Fs)
> {
>     auto dFiles = dirEntries(Fs, SpanMode.shallow).filter!(a => 
> a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
> 	foreach (d; dFiles)
> 	     {	Subdata ~=  d[0]; Subdata ~= d[1].toSimpleString; }
> 		return Subdata;
> 			
> }
> void main ()
> {
>  foreach (string Fs; Dirlst[0 .. $])
>     {
>                auto TestTask = task(&Test, Fs);
> 		TestTask.executeInNewThread();
> 		auto TestTaskData = TestTask.yieldForce;
> 		writefln("%-63s %.20s", TestTaskData[0], TestTaskData[1]);
> 	}
> }
>
> Output:
>
> C:\Temp\TEAM\Test.pdf                                    
> 2017-Aug-24 18:23:00
> C:\Temp\\PROD_TEAM\DND1.pdf                              
> 2017-Aug-25 23:38:04
>
>
> The folder C:\Temp\TEAM contains 2 files and folder 
> C:\Temp\\PROD_TEAM contain 4 files but it display only 1 file 
> per folder.

Hi,

  I was able to find the solution, thank you very much, please let 
me know if there are any good logic than below,

import std.file: dirEntries, isFile, SpanMode;
import std.stdio: writeln, writefln;
import std.algorithm: filter, map;
import std.array: array;
import std.typecons: tuple;
import std.parallelism;
string[][] Subdata;

auto Dirlst = [ "C:\\Temp\\TEAM" ];

string[][] CleanFiles (string Fs)
{
     auto dFiles = dirEntries(Fs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
	foreach (d; dFiles)
	        {
		     Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
			}
			return Subdata;
			
}
void main ()
{
  foreach (string Fs; Dirlst[0 .. $])
     {
         auto MCleanTask = task(&CleanFiles, Fs);
		MCleanTask.executeInNewThread();
		auto MCleanTaskData = MCleanTask.yieldForce;
		foreach(i; MCleanTaskData[0 .. $])
		writefln("%-(%-63s %)", i);
	}
	
}



From,
Vino.B




More information about the Digitalmars-d-learn mailing list