[Pdx-pm] Hash question

Randal L. Schwartz merlyn at stonehenge.com
Fri Nov 21 22:25:09 CST 2003

>>>>> "Bruce" == Bruce J Keeler <bruce at gridpoint.com> writes:

Bruce> This will recreate the hash, re-adding all the elements that were there
Bruce> before.  In cases where there are a lot of items in the hash to begin
Bruce> with, that's going to be inefficient.  Something like this might be
Bruce> better in that case:

Bruce>         %tmp = $db2->DataHash();
Bruce>         @SignUpInfo{keys %tmp} = values %tmp;
Bruce> Though now the new hash entries are going to be hashed twice instead, so
Bruce> that's less efficient in the case where there's more data being added
Bruce> than was there to begin with.

I think it's even been shown that iteration is better:

    my @array = $db2->DataHash();
    while (@array) {
      $SignUpInfo{shift @array} = shift @array;

Of course, I'm cheating here, knowing that the left side
is eval'ed before the right.  If you don't want that much magic:

    my %tmp = $db2->DataHash();
    while (my ($k, $v) = each %tmp) {
      $SignUpInfo{$k} = $v;

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

More information about the Pdx-pm-list mailing list