genie-logo

Gee collection datatypes

Page updated October 27, 2009
There is not yet an official logo for Genie/Vala, above image just a placeholder.

Introduction

If you are new to Genie, the first place to start is my page that introduces data types in Genie: Genie data types.

That page introduces the dict and list data types. More to follow as I expand this page. These datatypes are the "Gee collection datatypes" provided by the libgee package.

A starting point is the libgee homepage:
http://live.gnome.org/Libgee

API documentation:
http://references.valadoc.org/gee-1.0.html

When the libgee package is installed, you have the cryptic vapi file:
/usr/share/vala/vapi/gee-1.0.vapi

From this cryptic (to me) information, let's see what I can figure out...

The list datatype

These are the methods available (from the vapi file):

add, clear, contains, get, index_of, insert, set, remove, remove_at

Well, it may be that they are so simple to use that they don't need much documentation.

libgee
Don't forget one thing when you try these examples: the libgee library must be specified. For example:
prompt> valac --pkg=gee-1.0 example.gs
An example to try each function out:
init
var l = new list of string
l.add("abc")
l.add("def")
l.add("xyz")
if l.contains("mno") == false do print "mno is not in the list"
if l.contains("xyz") == true do print "xyz is in the list"
print "Content of field 1: %s", l.get(1)
l.set(1,"ghi")
print "Content of field 1: %s", l[1]
l.insert(1,"mno")
l.insert(1,"pqr")
l.remove_at(4) /*removes xyz*/
l.remove("mno")
print "Index of 'ghi': %d", l.index_of("ghi")
l.set(2,"stu") /*changes ghi to stu*/
for o in l do print o
Easy as pie! All of the above should be easily understandable.

Note that fields of the list are accessed in two different ways, by index-number or by value. The first entry in the list is index-number zero, and the get() remove_at() and set() functions require this.

See how easy it is to iterate through a list using the for statement. For an introduction to for and other control-flow statements (while, if, case, etc.), see my page Decisions and loops.

There are two ways that you can access a field using an index-number:
	print "Content of field 1: %s", l.get(1)
print "Content of field 1: %s", l[1]
The latter is the more generic syntax, that works on other data types, such as arrays. So, these are also equivalent:
	l.set(2,"stu")
l[2] = "stu"


The dict datatype

There are two entries per field, of the form key:value. You access a value via the key, not by an index-number.

These are the functions:
clear, contains, get, get_keys, get_values, remove, set

And here is an example:
init
var d = new dict of string,string
d["fruit"] = "apple"
d["animal"] = "dog"
d.set("plant","cactus")
d.set("animal","hippopotomus") /*changes from 'dog'*/
if d.contains("plant") == true do print "Key 'plant' is in dictionary"
print "%s", d.get("animal")
for o in d.keys do print o //old libgee use d.get_keys()
for o in d.values do print o //old libgee use d.get_values()
d.remove("animal")
Again, very easy. Note again the equivalent syntax:
	d["animal"] = "dog"
d.set("plant","cactus")
Notice also the for loop, that you can choose whether to iterate through either the keys or the values.

To access an entry in a dictionary, you do it by key. So to print the value in "plant":
	print "%s", d["plant"]
Another thing, if you are a Genie-beginner then you might not know. Where I have put a string in the code for key or value, Genie is also happy with a variable, for example:
	var k = "plant"
print "%s", d[k]





(c) Copyright 2009 Barry Kauler puppylinux.com, all reproduction rights reserved.