[Gambas-user] Accessing the nth item in a collection

Tobias Boege taboege at ...626...
Wed Jun 4 13:45:59 CEST 2014


On Wed, 04 Jun 2014, Jussi Lahtinen wrote:
> If I remember correctly Benoit warned that collections doesn't necessarily
> keep the items in order. But I don't know what would make it to lose the
> order. Maybe he will enlighten the issue.
> Anyway I suggest you use array of objects, which you declare extra property
> "Key".
> 
> On Wed, Jun 4, 2014 at 3:22 AM, B Bruen <bbruen at ...2308...> wrote:
> 
> > Occasionally, and unfortunately for me lately quite frequently, I have
> > found a need to access the first or last item in (or perhaps better "the
> > first or last item added to")  a collection regardless of key values (and
> > by extension I could envisage a need to access the nth item in a collection
> > regardless of the key value). There does not seem to be an elegant solution
> > to this - or maybe just that I am incapable of developing an elegant
> > solution to this.
> >
> > "First" can be achieved by using the For Each construct and
> > unconditionally Break'ing the iteration on the first loop. This smells like
> > using a sledge hammer to crush an ant.
> > "Last" I have achieved after a manner, but the mechanism is too
> > embarrassing to reveal.
> > "nth" I am reluctant to attempt.
> >
> > Any clues?
> >
> > --
> > B Bruen <bbruen at ...2308...>

AFAIK, Collection is a hash table and of course, you cannot assume a
particular order of keys in a hash table.

But Bruce apparently doesn't care about an *order* but only that the
elements can be addressed bijectively by the integers in [0, .Count).

But the collection isn't meant to allow you to do that because elements
are addressed by strings, not integers and the integer addressing isn't
meaningful to people other than Bruce in his particular application :-)

HOWEVER, and that's my proposal, an integer index does make sense in the
AvlTree class in gb.data which is otherwise similar to Collection (i.e.
normally uses strings as indices, etc.), because we can traverse the keys
in an AvlTree in order.

If you can use AvlTree instead of the normal Collection in your application,
I will add functionality to get the n-th element of an AvlTree. Otherwise,
we must hack Collection for this special purpose.

And I don't think Benoit will make that change mainline (because it really
has no sense, under normal circumstances, to use integers to address a
string-built hash table) -- or Benoit adds a .Data property, if that is
possible at all, similar to that of the array classes...?

I don't know but my offer holds.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list