0

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?

lf_araujo
  • 1,991
  • 2
  • 16
  • 39

2 Answers2

1

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 )
AlThomas
  • 4,169
  • 12
  • 22
0

In python

last_index = len(a) - 1

print a[last_index]

In vala or genie,

last_index = a.length - 1

print a[last_index]
SuperNova
  • 25,512
  • 7
  • 93
  • 64
  • It seems that the Gee.Array list does not have a len or length method... Neither I could find a len function at valadoc... – lf_araujo Jul 04 '16 at 10:42
  • @Luís, `An array is created by using "array of type name" followed by the fixed size of the array e.g. var a = new array of int[10] to create an array of 10 integers. The length of such an array can be obtained by the length member variable e.g. var count = a.length.` Source : https://wiki.gnome.org/Projects/Genie` Please let me know if this is wrong.. – SuperNova Jul 04 '16 at 10:46