On 2004/9/06, Larry Wall wrote: >Another possibility is that .[] always forces the "normal" view of an >array as 0-based, and if you want non-0-based arrays you have to use >the .{} interface instead, on the assumption that strange subscripts >are more like hash keys than ranges of integers.
That's true. But it's got me thinking about the connection between arrays and "associative" arrays. In fact, the user doesn't need to know that a "hash" is implemented with a hash table, and an "array" isn't; and nothing stops you from using numbers as hash keys. And if you restrict your "hash" to numeric keys, Perl could notice and optimise it into an array. (Or integer keys, or positive integers, or a consecutive range of positive ints....) If we consider a generic "data structure" type (which may or may not be optimised under the hood for integral indices), then why shouldn't {} be the "index-by-name" interface, and [] the "index-by-ordinal" interface? (Does that mean [$x] is just a shorthand for {nth($x)}?) (Will P6 arrays/hashes have an ".index" method to return what index they are? Or .index for the ordinal and .key for the name. (Although .key is perhaps a bit too close to .keys when the member is itself a hash.)) Of course, there is one important difference between arrays and hashes: arrays are ordered. People do keep asking about ordered hashes, though. There's no reason you couldn't use ordinals with a hash anyway (the "order" may not be particularly meaningful, but sometimes you don't care -- of course, in those cases you'd usually iterate through it, but it could be handy to be able to say %h[rand(last)] (except that isn't really quite how you'd say it, but you get the idea)). Data structures might have default sorting by key (since that's what arrays have), but you could sort other ways... maybe nth takes a "by" adverb: "first"="first :by key", vs. "first :by value". Hm, it probably should take a closure (along the lines of "sort"). Anyway, I'm rambling, but there's something to the idea of Perl offering some sort of generic "data structure" type... "my $hash is Struct;" would be the most general, no restrictions on keys, and no ordering, so Perl is free to use a hash table without worrying about the order. "my $array is Struct(keytype=>PosInt, ordering=>keywise)" has keys restricted to ints, and iterates in order by index, i.e. it's implemented as an ordinary array (where [$n] and {$n} refer to the same element). "Hash" and "Array" types would be shorthand for those kinds of Structs, but you could define your own by providing suitable shapes and orderings. (Hm, since Hashes are the most general, they're probably actually the base type, rather than "Struct", which does sound kinda silly, and probably sounds sillier if you're not used to C.) I was also going to suggest an in-between type of structure, like a Collection in VB, that accepts anything for the key (or some useful restricted type?) but is ordered (in order of when elements were added). But I can't think of any character available for the sigil. =) - David "making a hash of things" Green