Need a few examples, please

Ask questions about how to create a script or swap scripts with other users.
Post Reply
kyguy6969
Posts: 11
Joined: 01 Mar 2013 02:38

Need a few examples, please

Post by kyguy6969 »

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.
User avatar
pjj
Posts: 2207
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Need a few examples, please

Post by pjj »

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
User avatar
pjj
Posts: 2207
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Need a few examples, please

Post by pjj »

Here you are, re-using code from the first example (ad 2):

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();
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.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
kyguy6969
Posts: 11
Joined: 01 Mar 2013 02:38

Re: Need a few examples, please

Post by kyguy6969 »

Thanks loads!

I haven't been on the board for awhile. The example have given me a jump start on what I needed.
User avatar
pjj
Posts: 2207
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Need a few examples, please

Post by pjj »

I'm glad I could help! :)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
kyguy6969
Posts: 11
Joined: 01 Mar 2013 02:38

Re: Need a few examples, please

Post by kyguy6969 »

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.
User avatar
pjj
Posts: 2207
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Need a few examples, please

Post by pjj »

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);
It finds this pattern: colon, followed by two digits; then you have to strip off colon, of course. -- I hope this is what you wanted (the trick is to add slash before slash.)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
kyguy6969
Posts: 11
Joined: 01 Mar 2013 02:38

Re: Need a few examples, please

Post by kyguy6969 »

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.
User avatar
pjj
Posts: 2207
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Need a few examples, please

Post by pjj »

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):

Code: Select all

ShowMessage(length(Document.Lines[0]));
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 :?
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply