genie

How to find out the last item in a list of string?


Working with list of string is pretty straightforward in Genie. I was wondering if one could locate the last added item with something similar to [-1] in python.

Taking the example from Genie's tutorial:

[indent=4]

init

    /* test lists */
    var l = new list of string

    l.add ("Genie")
    l.add ("Rocks")
    l.add ("The")
    l.add ("World")

    for s in l
        print s

    print " "

    print l[-1]

Aim

My expectative was that the l[-1] bit would point to "World" item. However it gives me the error at execution time:

ERROR:arraylist.c:954:gee_array_list_real_get: assertion failed: (index >= 0)
/tmp/geany_run_script_X6D4JY.sh: line 7: 13815 Abortado                (imagem do nĂșcleo gravada)/tmp/./test

Question

The gee array clearly only works with positive indexes, is there any other way to get the last added item on an array?


Solution

  • Gee list has a last method:

    print( l.last() )
    

    A Gee list's length is found with the size property:

    print( l[ l.size -1 ] )
    

    A Gee list can also be sliced with only the last element:

    for s in l[l.size-1:l.size]
        print( s )