I need a few examples. Specifically:
1. Loop through a file from beginning to end, storing the current line in a variable.
2. Find a line with a specific text in it, select the line, and delete the line (including the newline character).
Thanks in advance.
Need a few examples, please
Re: Need a few examples, please
Here you are (ad 1):
Code: Select all
var s; // variable to store line of text
// turn off screen updating
Document.BeginUpdate();
// turn word-wrap off
if (Document.Wordwrap) {
Document.Wordwrap = false;
}
// loop thru lines
for (var i = 0; i < Document.LineCount; i++) {
s = Document.Lines[i];
// do something with s here
}
// turn on screen updating
Document.EndUpdate();
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Re: Need a few examples, please
Here you are, re-using code from the first example (ad 2):
OK, this may need some explanation:
1) needle is, of course, the text you're looking for
2) d[1000] means your document can only have 1000 lines (you need to define array size upfront; of course you can change this value to suit your needs)
3) ScriptUtils.Pos() -- you can use other function if you need regex, cfr. help file
4) delete excess lines at the end because you can't use Document.Clear and then build your document up anew line by line, as after clearing it line count would equal to 0; side effect of solution I used are duplicate lines at the end of document, which must be then cleared; this means that these empty lines are still there, but you can delete them automatically at document save
Hope that helps.
Code: Select all
var needle = "erase this";
var d[1000]; // variable to store document text w/o lines containing needle
var j = 0; // counter for d
var upto = Document.LineCount; // no. of lines in original document text
var x;
// turn off screen updating
Document.BeginUpdate();
// turn word-wrap off
if (Document.Wordwrap) {
Document.Wordwrap = false;
}
for (var i = 0; i < upto; i++) {
x = ScriptUtils.Pos(needle, Document.Lines[i]);
if (x == 0) { // no needle on this line
d[j] = Document.Lines[i];
j++;
}
}
if (i == j) {
ShowMessage("nothing found");
} else {
for (i = 0; i < j; i++) {
Document.Lines[i] = d[i];
}
// delete excess lines at the end of document
for (i; i < upto; i++) {
Document.Lines[i] = "";
}
}
// turn on screen updating
Document.EndUpdate();
1) needle is, of course, the text you're looking for
2) d[1000] means your document can only have 1000 lines (you need to define array size upfront; of course you can change this value to suit your needs)
3) ScriptUtils.Pos() -- you can use other function if you need regex, cfr. help file
4) delete excess lines at the end because you can't use Document.Clear and then build your document up anew line by line, as after clearing it line count would equal to 0; side effect of solution I used are duplicate lines at the end of document, which must be then cleared; this means that these empty lines are still there, but you can delete them automatically at document save
Hope that helps.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Re: Need a few examples, please
Thanks loads!
I haven't been on the board for awhile. The example have given me a jump start on what I needed.
I haven't been on the board for awhile. The example have given me a jump start on what I needed.
Re: Need a few examples, please
I'm glad I could help!
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Re: Need a few examples, please
The posts helped me greatly. Thanks again. However, I've run into one speed bump, and I need help. In a var, I have:
var temp = "12/10/2013 21:39 HOURS (ET) USA";
But when I try to get a sub string, I get an error. I've tried may, may variations to no avail. Can someone help? Say for instance I wanted the "39" out of the above. What do I do?
I've just probably brain farted.
Thanks in advance.
var temp = "12/10/2013 21:39 HOURS (ET) USA";
But when I try to get a sub string, I get an error. I've tried may, may variations to no avail. Can someone help? Say for instance I wanted the "39" out of the above. What do I do?
I've just probably brain farted.
Thanks in advance.
Re: Need a few examples, please
Code: Select all
var temp = "12/10/2013 21:39 HOURS (ET) USA";
var find = "";
x = ScriptUtils.RegExPos(":\\d{2}", temp, find);
ShowMessage(x);
ShowMessage(find);
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Re: Need a few examples, please
for (var i = 0; i < upto; i++)
{
x = ScriptUtils.Pos(needle, Document.Lines);
if (x == 0)
{
// no needle on this line
d[j] = Document.Lines;
j++;
}
}
This is an excerpt from pjj's post. What I'm now trying to do is to detect a string in d[j] that terminates in the new line character, say abcdefg<new line>. I've tried \n, and \r, and \r\n, and \n\r with no success. Can anyone help?
Thanks. Prefer CPP.
Re: Need a few examples, please
Document.Line doesn't contain newline -- it can be found out with this oneliner, which shows number of characters in a string (first line of document):
Therefore your way will never work: there are simply no abcdefg<new line> in the document, only abcdefg. Instead, you could use ScriptUtils.RegExPos(), but there is a serious glitch with strings now (cfr. this post) and I assume this is why code with RegExPos I just tried doesn't work for me
Code: Select all
ShowMessage(length(Document.Lines[0]));
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus