www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Help required on Array appender

reply Vino.B <vino.bheeman hotmail.com> writes:
Hi All,

  Can you please guide me how can i use array appender for the 
below piece of code

string[][] cleanFiles (string FFs, string Step) {
auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
	foreach (d; dFiles) {
		     if (Step == "dryrun")
			    { Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]]; }
				  else if (Step == "run") {
					remove(d[0]);
					if (!d[0].exists)
					Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
						}
			}
			return Subdata;
}

From,
Vino.B
Sep 02
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 2 September 2017 at 10:15:04 UTC, Vino.B wrote:
 Hi All,

  Can you please guide me how can i use array appender for the 
 below piece of code

 string[][] cleanFiles (string FFs, string Step) {
 auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
 a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
 	foreach (d; dFiles) {
 		if (Step == "dryrun")
 		{
                     Subdata ~=  [d[0], d[1].toSimpleString[0 .. 
 20]];
              }
 		else if (Step == "run")
              {
 			remove(d[0]);
 			if (!d[0].exists)
 				Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
 		}
 	}
 	return Subdata;
 }

 From,
 Vino.B
If you're wanting to use appender just make an appender and replace the ~= to calls to appender.put(data); if you're trying to make it faster, consider that Step could be a bool, your return type could be string[2][], the `if (!d[0].exists)` is redundant since `remove` will throw if it fails. That leaves you with string[2][] cleanFiles(string FFs, bool dryrun) { auto dFiles = dirEntries(FFs, SpanMode.shallow) .filter!(a => a.isFile) .map!(a =>[a.name , a.timeCreated.toSimpleString[0 .. 20]) .array; if (! dryrun) dFiles.each!(f => f[0].remove); }
Sep 02
next sibling parent Azi Hassan <azi.hassan live.fr> writes:
On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas Wilson 
wrote:

 If you're wanting to use appender just make an appender and 
 replace the ~= to calls to appender.put(data);
Just making Subdata an Appender!(string[][]) (or Appender!(Tuple!(string, string)[])) is enough since it already overloads the ~= operator. Performance aside, a small nitpick is that it's possible to write filter!isFile instead of filter!(a => a.isFile) since isFile only accepts one argument.
Sep 02
prev sibling parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas Wilson 
wrote:
 On Saturday, 2 September 2017 at 10:15:04 UTC, Vino.B wrote:
 Hi All,

  Can you please guide me how can i use array appender for the 
 below piece of code

 string[][] cleanFiles (string FFs, string Step) {
 auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
 a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
 	foreach (d; dFiles) {
 		if (Step == "dryrun")
 		{
                     Subdata ~=  [d[0], d[1].toSimpleString[0 
 .. 20]];
              }
 		else if (Step == "run")
              {
 			remove(d[0]);
 			if (!d[0].exists)
 				Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
 		}
 	}
 	return Subdata;
 }

 From,
 Vino.B
If you're wanting to use appender just make an appender and replace the ~= to calls to appender.put(data); if you're trying to make it faster, consider that Step could be a bool, your return type could be string[2][], the `if (!d[0].exists)` is redundant since `remove` will throw if it fails. That leaves you with string[2][] cleanFiles(string FFs, bool dryrun) { auto dFiles = dirEntries(FFs, SpanMode.shallow) .filter!(a => a.isFile) .map!(a =>[a.name , a.timeCreated.toSimpleString[0 .. 20]) .array; if (! dryrun) dFiles.each!(f => f[0].remove); }
Hi, Thank you very much, and your idea help a lot, and the reason to use appender is for both faster and performance as my program use's many ~= function and found that using appender is the best way when compared with the ~= e.g;(MCresult.get ~= MCleanTaskData;) void mCleanFiles (string[] Dirlist, File logF, File logE, string Step) { try { string[][] MCtext; string[][] MCEresult; auto MCresult = taskPool.workerLocalStorage(MCtext); logF.writeln("Function \t : List of the File's which are not placed in correct Location and list those deleted"); logF.writeln("Dir. Scanned \t :", Dirlist); logF.writeln("************************************************************************************"); logF.writefln("%-63s %.20s", "File Name", "CreationTime"); logF.writeln("************************************************************************************"); foreach (string Fs; parallel(Dirlist[0 .. $], 1)) { auto FFs = Fs.strip; auto MCleanTask = task(&cleanFiles, FFs, Step); MCleanTask.executeInNewThread(); auto MCleanTaskData = MCleanTask.workForce; MCresult.get ~= MCleanTaskData; } foreach(i; MCresult.toRange) logF.writefln("%(%-(%-63s %)\n%)", i.sort!((a,b) => a[0] < b[0]).uniq); logF.writeln("************************************************************************************"); } catch (Exception e) { logE.writeln(e.msg); } } From, Vino.B
Sep 02
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 2 September 2017 at 15:47:31 UTC, Vino.B wrote:
 On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas Wilson 
 wrote:
 [...]
Hi, [...]
Hi, Was able to resolve the above issue, but again getting the same for other lines such as below when i tried to add the appender. auto CleanDirlst = appender([]); CleanDirlst ~= PVStore[1][i].to!string.split(","); Error: cannot implicitly convert expression appender([]) of type Appender!(void[]) to string[].
Sep 02
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 2 September 2017 at 21:11:17 UTC, Vino.B wrote:
 On Saturday, 2 September 2017 at 15:47:31 UTC, Vino.B wrote:
 On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas Wilson 
 wrote:
 [...]
Hi, [...]
Hi, Was able to resolve the above issue, but again getting the same for other lines such as below when i tried to add the appender. auto CleanDirlst = appender([]); CleanDirlst ~= PVStore[1][i].to!string.split(","); Error: cannot implicitly convert expression appender([]) of type Appender!(void[]) to string[].
You want `Appender!(string[])`
Sep 02
parent Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 2 September 2017 at 22:39:33 UTC, Nicholas Wilson 
wrote:
 On Saturday, 2 September 2017 at 21:11:17 UTC, Vino.B wrote:
 On Saturday, 2 September 2017 at 15:47:31 UTC, Vino.B wrote:
 On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas 
 Wilson wrote:
 [...]
Hi, [...]
Hi, Was able to resolve the above issue, but again getting the same for other lines such as below when i tried to add the appender. auto CleanDirlst = appender([]); CleanDirlst ~= PVStore[1][i].to!string.split(","); Error: cannot implicitly convert expression appender([]) of type Appender!(void[]) to string[].
You want `Appender!(string[])`
Hi, Thank you very much, was able to resolve the issue.
Sep 02