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