www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Appending data to array results in duplicate's.

reply Vino.B <vino.bheeman hotmail.com> writes:
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"]

From,
Vino.B
Aug 25 2017
next sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 25 August 2017 at 16:45:16 UTC, Vino.B 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"]

 From,
 Vino.B
You are consecutively appending to an array in each loop iteration step, i.e. you are buffering, and you're printing the current state of the buffer (all previously buffered elements) in each loop iteration. I can't see any duplication going on, what exactly to you wish to accomplish?
Aug 25 2017
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Aug 25 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
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.
Aug 25 2017
parent Vino.B <vino.bheeman hotmail.com> writes:
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
Aug 25 2017