Mark J. Reed wrote:
> The point is that %h[0] should be legal syntax that works on any hash,
> returning the "first" element. It doesn't matter if the elements are
> sorted or even sortable.  You get an element.  More to the point, if
> you don't add any elements to (or remove any elements from) the hash,
> you get the *same* element every time you ask for %h[0], which is
> different from %h[1], etc - all this is by default, of course.

True enough.

> But is %h[0] a value or a pair?  Or does it depend on context?

%h[0] should return a value; if you want a pair, say '%h[0]:p'.  (The
same should - and does? - hold true for arrays: @a[0] returns a value,
while @a[0]:p returns an index/value pair.)

The single messiest part of this concept is that %h[] should be aware
of what the hash's keys are, and %h{} should be aware of what the
indices are.  This is so that you can mix subscripts with hashes in
the same way that you can with arrays: see S09, "Mixing subscripts".

The biggest difference between %h and @a, then, is that @a is much
more rigid when it comes to indices: the same index always points to
the same item, and keys have to be pre-established.  With %h, keys can
be added or removed at will; but each time you do so, the hash gets
re-indexed: so %h[0] potentially shifts to another item every time the
keys are changed.  This latter point is an example of what the
"Autosorted hashes" section in S09 is addressing with its talk about
the downside of autosorting a hash.

Jonathan "Dataweaver" Lang

Reply via email to