How to optimally write contents of array to a file

Help with writing and running scripts

How to optimally write contents of array to a file

Postby gdeleon » Tue Sep 07, 2010 10:38 am

I have an array of strings and want to flush out to a new file. Is there a way to optimally write the contents of the array to a file?

I know the following will do what I want, but I want to do it faster:

for (var i = 0, len = myResults.length; i < len; i++)
{
UltraEdit.activeDocument.write(myResults[i]);
}
gdeleon
Newbie
 
Posts: 2
Joined: Tue Sep 07, 2010 10:27 am

Re: How to optimally write contents of array to a file

Postby Mofi » Tue Sep 07, 2010 11:59 am

Well, that depends on what type of array myResults is. Is it an array of integer values, of characters or of strings? Do you want every element of the array on a new line or not?

Let us assume myResults is an array of strings and you want every string on a new line and the strings do not contain line terminations. In this case it would be best to concatenate all the strings first to a single string and then output this very long single string.

var sOutput = myResults.join("\r\n");
if (sOutput.length) UltraEdit.activeDocument.write(sOutput);

Scripts executed with UltraEdit are fast when they avoid display updates as much as possible by doing as much as possible in memory and as less as possible in the active document window or any other document window partly visible.

The JavaScript core method join("") of the array object can be used also for arrays with integer values or characters.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to optimally write contents of array to a file

Postby gdeleon » Wed Sep 08, 2010 11:22 am

Thank you! That reduced my elapse time by 25%.
gdeleon
Newbie
 
Posts: 2
Joined: Tue Sep 07, 2010 10:27 am


Return to Scripts