Tapatalk

Creating an array from multiple text lines

Creating an array from multiple text lines

2
NewbieNewbie
2

PostJul 20, 2011#1

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?

6,825625
Grand MasterGrand Master
6,825625

PostJul 20, 2011#2

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.

2
NewbieNewbie
2

PostJul 21, 2011#3

Thank you very much Mofi, you're last script does exactly what i want.