in reply to Sparse arrays?

I have always thought that sparse arrays was just another term for a hash.

-enlil

Replies are listed 'Best First'.
Re: Re: Sparse arrays?
by sauoq (Abbot) on Dec 18, 2002 at 17:33 UTC
    I have always thought that sparse arrays was just another term for a hash.

    No. Hashes are unordered collections. You can impose an order on them by requiring ordered keys and then use a hash to implement a sparse array. Additional information, such as a normal dense array containing the sorted hash keys, are needed in order to allow iteration though.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Additional information, such as a normal dense array containing the sorted hash keys, are needed in order to allow iteration though.

      Of course you can obtain that on the fly trivially with foreach( sort { $a <=> $b } keys %sparse ). It's really more of an efficiency question whether you want to keep that seperately or do it each time (and either way could be done transparently with a tied array).