Showing posts with label []. Show all posts
Showing posts with label []. Show all posts

Monday, May 7, 2007

Lesson 4: The Extra Mile

For me string variables are the sonic screwdriver of coding. I use them to build not only the functions I need but the logs that monitor them, so being able to manipulate them is crucial for doing the things I want.

Let's take a look at the whole log file concept. I have a text file that is filled with lines of text something like this:
Lacedaemonians|60|Menelaus
Boeotians|50|Thersander
Minyans|30|Ascalaphus
Arcadians|60|Agapenor
The first thing you need to do is get the file data into a string so you can start modifying it.
str a b c
int z

a.getfile("C:\qm\ships.txt")
Now lets get just the last line of that text file by using two different functions.
b.getl(a (numlines(a)-1) 2)
out b
The 'getl' function first asks you for the string from which you want to get the line (variable 'a'). The second part is a little trickier; the 'numlines' function tells you how many lines are in the string 'a', but the '-1' needs some explaining. The 'getl' function is what is called 'zero-based' which means (it's terribly annoying and prone to 'one-off' mistakes) it starts counting at '0' not '1' just like a computer....um...ok yeah, that does make sense now but it's still annoying. So, it has 4 lines but the last line is 3 hence the '-1'. So now 'b' contains the last line of 'a'.

Now, let's remove the line that we loaded into 'b' and put it back into 'a' but in the 2nd position.
a.RemoveLineN(numlines(a)-1);;this removes the line at the end (that we just copied) so it won't be duped.
a.InsertLineN(b 1);;again zero-based function.

out a

For the next step, let's add a new line. This new line can come from anywhere i.e. a file, the clipboard, or text selection, but here I've just set 'b' to it and then dropped it onto the end of 'a'.
b="Salamis|12|Telamonian Ajax"
a.addline(b 1)
out a
A note about the flag used on line two (not zero-based 80) ). The '1' tells 'addline' to not add a 'new line' character at the end of 'a'.
A note about flags: flags are just parameter codes for functions that allow you to modify the basic behavior of a function.

Now let's say you want to pull out the last part of each string and take a look at it (e.g. Menelaus, Thersander, Ascalaphus, etc). Here's how to set up the loop and grab the last section.
rep numlines(a)
,b.getl(a)
,c.gett(b 2 "|[]")
,out "c=%s" c
Note: the commas are another way of indenting the code within the QM Editor. However, when you copy/paste the code into another program, the tabs are replaced with commas. If you copy the code above to your QM Editor, you'll get the commas unless you use the menu Edit -> Other formats -> Paste escaped option.

Now, about the code, I'm using the 'numlines(a)' instead of checking for the error code output from the 'getl' just as a matter of preference (I'll explain more about that in a minute). The 'getl' is missing the line number to pull but when that value is missing, it just pulls the next line; you could use an incrementing integer but this is simpler. If, however, there is no next line, the 'getl' function returns -1; it would be that -1 that you would check for if you didn't know exactly how many lines where in the variable. The real money maker is the 'gett' or 'get token'. Think of a token as a 'chunk' of the string. To break up each line, I've used the pipe character '|' as my delimiter because it was very unlikely to show up in the data. But I also have the '[]' within the 'gett' command in its delimiter section. This tells QM that the last chunk of data in a line will end at the new-line character. Otherwise, it will grab the next line's first section (you can try it out to see how it affects the data pull). And finally, you'll notice that through out this lesson I've been using the 'out' command to monitor my changes in the data but I've used a modified version of this at the end to include some prefix text to identify the variable. This technique really comes in handy when I'm debugging and vetting several variables at once and it can contain many variables on the same line as well as identifying them.

So there you have it, some of the really useful string functions. And if you really want to turn it up, you can start getting into using 'replacerx' and Regular Expression coding...not that I would know anything about that. ;o)

So have at it and I'll see you next time cause the hook always brings you back.


BTW: Here's the entire code block to make it easy to import into the Editor (don't forget to use the Paste-Escaped option).

ClearOutput
str a b c
b.getl(a (numlines(a)-1) 2)
a.RemoveLineN(numlines(a)-1)
a.InsertLineN(b 1)
b="Salamis|12|Telamonian Ajax"
a.addline(b 1)
out a
rep numlines(a)
,b.getl(a)
,c.gett(b 2 "|[]")
,out "c=%s" c


Thursday, May 3, 2007

Lesson 4: String Theory

Sorry, this post won't have anything to do with gravity wells, point particles, or CERN so you can keep your boson particle "foam fingers" in your truck (however, I do like me some tailgate grilling action so feel free to fire up that Grill Master 3000 and make my buffalo steak well-done ... WOOT!). Though I can't tell you the foundation of reality or, more importantly, what's underneath reality that is so sturdy as to support reality's foundation, I can say this, "Variables are the foundation of coding" and especially so in macros. So crack open that 12-pack of Diet Coke and toss 'em in the cooler and let's get this party started.

To first use a variable, you'll need to declare it by telling QM what kind it is and what you want to call it. Here's what it looks like in the wild.
str a
That was easy enough but what can you DO with it? Well, let's start by giving it some values; which can be done several different ways.

str a="anything you want"
Note: the use of the double-quotes.

str a.getclip
This gets the contents of the clipboard.

str a.getsel
This gets the currently selected text.
str a.getwintext(win)
This gets the tittle-bar text of the currently active window.

Now let's put 'em into play. Let's say you want to create a macro that will grab the url of the current web page that you are on and send an email to your buddies (cuz they don't get enough of that stuff from their moms). In FireFox and I.E. 7, if you hit Alt-d the address bar is given the focus and the entire contents are selected. Let's put that into a string variable and the combine it with another string variable and then copy all that to the clipboard ready to paste into an email. Here's how we do it.


str url mesText
mesText=
;Hey guys you gotta check this out.
;
'Ad
url.getsel
mesText.from(mesText url)
mesText.setclip



Now there are some strange things going on in there so let's take a look.

First, line one is a shorthand way of creating more than one variable on one line. Second, the variable mesText is assigned a value rather oddly. If you set it up like this, every line that follows the 'mesText=' line that has a semi-colon or a space in the front (in other words, that are marked as 'comments' in the code) are assigned as the variable value. This allows you to set big blocks of text without it running all the way across the page and manually entering in the line-breaks (BTW: use [] as the escape-characters for a line-break in QM). Moving on, we have the 'get selection' function (this gets the url). And then, we have the 'from' function. Now, this is a handy one. 'From' allows you to combine (concatenate) two or more string variables and or text 'chunks' that are surrounded by double-quotes. To say it in a human language you would say, "Tack on 'url' to the end of 'mesText'." And then it takes the new value of mesText and loads it to the clipboard. All you have to do is: open a new email; paste it into the body; add a subject; and address it.

Ummmm....yeah....that's too much freakin' work! So, let's turn up the juice and see what shakes loose (MAN, I love quoting old 80's flicks!).

str url mesText mailto subj ;;I'm creating a bunch of variables at one time
mailto="mailto:Ender@battle-school.mil;Bean@battle-school.mil?subject=subHere&body="
subj.getwintext(win)
subj.findreplace(" - Mozilla Firefox" "");;I'm just pulling out the 'Mozilla Firefox' part cuz I don't need it
mailto.findreplace("subHere" subj);;I'm injecting the url into the subject parameter of the mailto command
mesText=
;Hey guys you gotta check this out.%0D%0D
'Ad
url.getsel
mailto.from(mailto mesText url);;I'm 'stringing' them all together here
run mailto


Oh yeah, now that's lazy with a capital "L" baby efficient and helpful; besides, you've got better things to do than menial cut/paste you've got to defend the Frontier against Xur and the Ko-Dan Armada!

So have at it and I'll see you next time cause the hook always brings you back.