Groovy 101

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 :

println “Hello World”

and we should see a result like this :

Hello World

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

“Hello World”

then the result is shown with the double quotes removed as the text moves to the screen :

Hello World

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 :

String fn = “readme.txt”
File f = new File(fn)
int linecount = 0
f.eachLine{it -> println it; linecount += 1; }
println linecount+” lines”

and for desert, the output would look like this :

Mary
Harry
Why
Should I asked
Mary
5 lines

Kinda nice, yes ? and if we didn’t want to indicate a variable type, we could use a ‘def’ aka ‘define’ and code this :

def fn = “readme.txt”
def f = new File(fn)
def linecount = 0
f.eachLine{it -> println it; linecount += 1; }
println linecount+” lines”

or this ( look ma – no def’s ) :

fn = “readme.txt”
f = new File(fn)
linecount = 0
f.eachLine{it -> println it; linecount += 1; }
println linecount+” lines”

or if you’re lazy like me we could save a line and declare the filename this way :

f = new File(“readme.txt”)
linecount = 0
f.eachLine{it -> println it; linecount += 1; }
println linecount+” lines”

or reducing the number of lines of code, we could still run this too :

linecount = 0
new File(“readme.txt”).eachLine{it -> println it; linecount += 1; }
println linecount+” lines”

this is the minimum code fragment that will  do the trick :

linecount = 0
new File(“readme.txt”).eachLine{println it; linecount += 1; }
linecount+” lines”

Any of these code fragments will produce the same result :

Mary
Harry
Why
Should I asked
Mary
Result: “5 lines”

=====================

For something similar, we could use a collection – a sequence of ‘things’ and try :

somebox = []
new File("readme.txt").eachLine{ somebox << it + '\n' }
println somebox
println somebox.size()+" lines"

and the somebox variable stores every line from the textfile. We can then use the .size() method to discover the count of lines :

[Mary
, Harry
, Why
, Should I asked
, Mary
]
5 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.

One thought on “Groovy 101

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s