On Sat, Jun 22, 2002 at 11:42:40PM -0500, Shawn wrote:

> >>> On Sat, 2002-06-22 at 13:53, Shawn wrote:
> >>
> >> my %hash = (user => 1,pass => 2,thing => 3,ptr => [qw(4 5)],);
> >>
> >> my $hash_ref = \%hash;
> >>
> >> print split(/ /,%$hash_ref ), "\n";
> >> print "Not Split: ", %$hash_ref, " :\n";
>  >> my @array = %$hash_ref;
> >> print "Array Game: @array :\n";
> >>
> >> print " Player: @{ [  %$hash_ref ] }\n";
> >>
> >> generates
> >>
> >> 4/8
> 
> >
> > This is the number of buckets used over the number of buckets.  Probably
> > not terribly useful unless you are optimising hashing algorithms.  In
> > this case it shows that perl has done a pretty good job with the hash.
> 
> 
> Can you elaborate more on the 'buckets' Paul, or point me to a doc
> that explains it more?  All the hash elements should be (are) filled
> when I dereferrence the hash in PACKAGEB.  So why would I ever see
> 1/8?

Any half decent reference on data structures should be able to explain
about hash tables.  A common implementation is to have an array of
linked lists.  A hashing function allocates the (key, value) tuple to a
list depending on the value of the key.  In this case each linked list
is a bucket, and the size of the array is the number of buckets.

4/8 tells me that there are 8 buckets (linked lists) in this hash, and
that 4 of them are used.  With only four elements in the hash this is
good, because each element will be the first in its bucket and so hash
access will be fast.

1/8 tells me that of the 8 buckets only one is used.  Whether this is
satisfactory will depend on the number of elements in the hash.  In the
degenerate case that all elements are in the same bucket the hash will
behave like a linked list.  Perl's hashing function has been designed to
make this unlikely, even with machine generated keys which are often the
Achilles' heel of hashing algorithms.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to