Pls Excuse Me While I Waffle
Computers are dumb, a bit like Fido. They have to be told what to do. ‘Ok, fetch’, ‘Chase the cat’, ‘go to sleep’. We can train our digital Fido using statements and commands. If we put commands together into a series of steps, our digital friend will be able to do tricks, assuming he recognises each command. That series of steps is a program. When recorded, it forms a repeatable set of things to be done. Working with our digital Fido requires the ability to give him commands, something like a keyboard (or mouse), and a way for Fido to respond, often by making a sound, or producing a response on a computer screen/panel/terminal or some such. Keyboard+display= simple, usable means of getting our ideas into the digital realm.
The name ‘Groovy’ describes a language syntax or set of commands we can put together to make something happen in a computer. It’s a bit like the old basic language we could use to program Commodore C64 and Radio Shack TRS80 personal computers, all those years ago!
There are a few things you’ll need to do in order to make your computer understand this set of commands. We’ll cover that later. I just want to give you a taste of Groovy, not bore you with technical details at this point.
Walking The Dog
It’s often useful to have your program talk to the outside world. Almost every computer language has some way to allow this. It’s often some variation of the word to show or display or print. Groovy uses the java approach, the print line command, or println, for short. What needs to be said is often enclosed in quotes, so the dumb computer will treat it as one statement.
So as promised, the Groovy way to say hello is :
and we should see a result like this :
Hmmm. I didn’t know we could do something like this until i just tried it, but the results of your last command are returned to your computer screen, so if the last of a set of commands is just
then the result is shown with the double quotes removed as the text moves to the screen :
A simple sample, no doubt. But not much use, really. Since a computer can hold a file of text, we often want to see what that text is. Textfiles are arranged into lines and given a name so the computer’s filing system can remember where it is. Text file names may have an appendix to the name that implies what’s in the file. This appendix is typically ‘txt’.
So now for the pasty resistance, let’s do something more complex; let’s print the content of a text file named readme.txt using a bit of Groovy :
and for desert, the output would look like this :
Kinda nice, yes ? and if we didn’t want to indicate a variable type, we could use a ‘def’ aka ‘define’ and code this :
or this ( look ma – no def’s ) :
or if you’re lazy like me we could save a line and declare the filename this way :
or reducing the number of lines of code, we could still run this too :
this is the minimum code fragment that will do the trick :
Any of these code fragments will produce the same result :
For something similar, we could use a collection – a sequence of ‘things’ and try :
and the somebox variable stores every line from the textfile. We can then use the .size() method to discover the count of lines :
Ok, not an exact match, but an alternative. It gives us a choice of how to do things.
Next time, i want to look at the Groovy setup requirements for Mac o/s, *nix choices like Ubuntu, and windows XP.