Sometimes, one might want to know what the value of duplicate lines might be in a file rather than simply using DELETE DUP of SORT UNIQ to eliminate them. In a large file, this can be difficult and tedious.
This macro will step through a pre-sorted file and create a file EXTDUPS.TXT containing the duplicate lines. Unlike DELETE DUP, a start column and end column can be used as arguments to limit the compare to specific columns in each line.
Any suggestions for improvement would be appreciated. I toyed with the idea of copying the output buffer to the clipboard followed by issuing SPF_CMD("CLIP") in the hope that it would open a new tab containing the duplicate lines, but it doesn't seem to work.
UPDATE: 10/03/2014 Added numeric verification of the column arguments. Duplicates are also saved to the Windows clipboard. Issue CLIP command to view them after macro completes. UPDATE: 10/04/2014 Added 3rd optional argument which allows user to specify whether to store duplicates in a file or Windows clipboard (Default). Added additional documentation.
That's a pretty ambitious first attempt for a macro. Your attempt to do SPF_CMD("CLIP") won't work, like a command of SPF_CMD("EDIT filename") won't work either. It has to do with the fact that SPFLite basically suspends itself for the duration of a macro, so that only the thinBasic interpreter is running, not SPFLite. When the macro terminates, SPFLite resumes. For the call to SPF_CMD to function, it has to be something that SPFLite can do while in this suspended state. There are limits to which things can be done. Essentially you can apply any primary command to the CURRENT edit session, that is, the one that launched the macro. You can't start another session, jump to an existing session, etc. because that would change the operational context under which the interpreter and SPFLite are running in.
You could put your data into the clipboard, and, once the macro finishes, issue a CLIP command yourself, either manually or with a mapped key.
I have had discussions with George about putting some commands like CLIP and EDIT into a queue, so that they would automatically run after a macro completed. So far, he thought the implementation effort would exceed its usefulness. Perhaps your experiences might convince him to reconsider. [He should be back from vacation in about 2 weeks.]
To check for numeric, use the VERIFY function in thinBasic. Issue the command HELP BASIC from the primary command line to get into the Basic help. Click on thinBasic Modules -> Core -> String Functions -> Verify.
To see if string ABC is numeric, you say IF VERIFY (ABC, "0123456789") = 0 THEN ...
VERIFY returns the string position within the first argument where a character is found that is not contained in the second argument. If no such non-compliant character is found, it returns 0. So, the return value answers the question, "where is the non-numeric character" or "where is the 'bad' character". An answer of 0 means "there aren't any bad characters".
Terry, it's good to see you trying these things. To be honest, most users don't try to create edit macros, and those that do keep them very simple. Plus, the thinBasic language is actually pretty sophisticated, and while we did our best to test things, we couldn't possibly test everything.
So, if you are going to experiment with this, I'd strongly recommend reading as much of the thinBasic documentation as you can (at least the "core" part for certain) if you haven't already. And, if you run into interesting situations or just have something to share with the group, please let us know. George and I know a lot about the SPFLite world, but we don't know everything about the macro part, since it's someone else's technology that we bolted on to get us a macro interpreter. You could very well discover things that no one else knows. It would be good to hear about your experiences.
You can thank (or blame) me for a lot of that. I have been nagging George for years to add stuff. You don't really need to "think" of doing something with a macro. It's driven by application requirements. Where you need macros is often if you are transforming some very large file with repetitive actions, and you get sick and tired of doing it manually. There is less of a need for it in modifying programs, but if you have big text data files and you are trying to convert it in some special way, the normal features of SPFLite won't do it alone, because SPFLite knows nothing about your data - only you do.
Another feature of SPFLite, which we had before macros, is "filters". You can do a lot with filters. If you have some transformational work to do, and you have an existing program that reads STDIN and writes STDOUT, you can usually use it as an SPFLite filter.