"Stephen P. Potter" <[EMAIL PROTECTED]> writes:

> Lightning flashed, thunder crashed and Russ Allbery <[EMAIL PROTECTED]> whispere
> d:
> | > Arrays are ordered.  Hashes are not.  Sure, you can iterate over a hash,
> | > but add an element to one and you can change the order of everything in
> | > it.
> | 
> | Formally, I believe it's permissable for a hash implementation to return a
> | different order the second time you iterate through it from the first

Actually not.  See "perldoc -f values":

] ...The actual random order is
] subject to change in future versions of perl, but it is guaranteed to
] be the same order as either the C<keys()> or C<each()> function would
] produce on the same (unmodified) hash...


> 
> What stops us from imposing order on this chaos?  If they are currently
> defined as not having any specific order, why can't we say they always
> return in numeric || alphabetic || ASCII || whatever order we want?

Efficiency.  If I just want to map over all elements stored in a hash, 
I just want the fastest access to all the elements.  If I want to
access the hash by ASCII order of keys, I can `sort keys %hash'.

Think of the keys as being returned in some "efficient" order.  The
specific "efficient" order is not specified, but it's guaranteed to be 
a fast way to iterate over all keys.

> The internal storage implementation can be kept seperate from the external
> interface.  A hash *should* be able to be advantageous over an array in
> just about all respects, except maybe size.  And, we've shown how a sparse
> array can be handled in a hash without any special machinations.

    for (@array) {
      # stuff with $_
    }

is fast.

    for (values %hash) {
      # stuff with $_
    }

(or the roughly equivalent for loop with `keys %hash' and while loop
with `each %hash') is fast.

    for (sort keys %hash) {
      # stuff with $hash{$_}
    }

is not so fast.  So defining a sorted order would negate the advantage 
of hashes over arrays you refered to.

It seems like a tied hash would be suitable for what you want.  If
ties were efficient, it would be trivial to implement a SortedHash
which would perform "each"-style accesses in sorted order.  Standard
Perl library, anyone?

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            | [EMAIL PROTECTED]
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem) \ We recycle all our Hz
72 Pinhas Rosen St.    |Tel: +972-3-7658514 (Main office)`---------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555    http://3w.compugen.co.il/~ariels

Reply via email to