Creating an array from multiple text lines

Help with writing and running scripts

Creating an array from multiple text lines

Postby moonraker » Wed Jul 20, 2011 7:20 am

I do have a simple text file with multiple lines of text.

My goal is to have a script which creates an array (in a new file) with those text lines. Every single text line should be a new entry of the array:

I've tried something, but I was not able to read in the text lines to the array.

Code: Select all
UltraEdit.newFile();

for (var i=0; i<=NumberOfTextLines; i++)
{
   UltraEdit.activeDocument.write('\noSeminarDe[' + i + '] = \"' + TextLine[i] + '\";');
}

Has somebody an idea idea how this could work?
moonraker
Newbie
 
Posts: 2
Joined: Wed Jul 20, 2011 7:09 am

Re: Creating an array from multiple text lines

Postby Mofi » Wed Jul 20, 2011 10:16 am

I can offer 3 solutions. The first one is a macro which I quickly recorded and then modified a little (IfColNum block inserted).

Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Clipboard 9
SelectAll
Copy
NewFile
Paste
IfColNum 1
Key BACKSPACE
EndIf
Top
ColumnModeOn
ColumnInsert "#"
Top
ColumnInsertNum 0 1 LeadingZero
ColumnModeOff
Bottom
InsertLine
Top
Find RegExp "%^([0-9 ]+^)#^(*^)$"
Replace All "oSeminarDe[^1] = "^2";"
ClearClipboard
Clipboard 0

The LeadingZero parameter is optional. You could remove it if the array index should be with spaces between number and closing square bracket ]. This macro could be also coded as script. It uses column mode editing feature and a tagged regular expression replace.

A similar macro without tagged regular expression.

Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Clipboard 9
SelectAll
Copy
NewFile
Paste
IfColNum 1
Key BACKSPACE
EndIf
Top
ColumnModeOn
ColumnInsert "oSeminarDe[] = ""
Key Ctrl+RIGHT ARROW
Key RIGHT ARROW
ColumnInsertNum 0 1 LeadingZero
ColumnModeOff
Bottom
InsertLine
Top
Find RegExp "$"
Replace All "";"
ClearClipboard
Clipboard 0

The third solution is a script based on what you thought about, but optimized to do as much as possible (= everything) in RAM. No leading zeros or trailing spaces applied for aligning the array index numbers, although that would be also possible with some extra code.

Code: Select all
UltraEdit.activeDocument.selectAll();
var sEntireText = UltraEdit.activeDocument.selection;
UltraEdit.activeDocument.top();
var asLines = sEntireText.split("\r\n");
if (asLines[asLines.length-1] == "") asLines.pop();
var sArrays = "";
for (var nIndex = 0; nIndex < asLines.length; nIndex++) {
   sArrays += "oSeminarDe[" + nIndex.toString() + "] = \"" + asLines[nIndex] + "\";\r\n";
}
UltraEdit.newFile();
UltraEdit.activeDocument.write(sArrays);
UltraEdit.activeDocument.top();

The script is written for DOS terminated lines.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Creating an array from multiple text lines

Postby moonraker » Thu Jul 21, 2011 1:19 am

Thank you very much Mofi, you're last script does exactly what i want.
moonraker
Newbie
 
Posts: 2
Joined: Wed Jul 20, 2011 7:09 am


Return to Scripts