Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting Btree by descending numeric values
    primarykey
    data
    text
    <p>I have been given a crazy problem to solve for a job interview. They expect me to deliver working code that will solve a fair sized problem on their system. However, I am supposed to develop this code on my system, not theirs. They have not told me what version of Perl they are using, and there is no way I can create the databases containing tens of millions of records they are going to run my code on. So basically, I am flying blind and I have no way to test my code.</p> <p>The particular problem that I can't seem to Google my way out of is how to sort a DB_FILE DB_BTREE in descending order by numeric values. After processing millions of records of domain names I am supposed to pick out the top fifty. Here is the code I am using:</p> <pre><code>$DB_BTREE-&gt;{'compare'} = \&amp;compare_values; unlink "domain_count_btree"; tie my %domain_count, "DB_File", "domain_count_btree", O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Cannot open file 'domain_count_btree': $!\n"; </code></pre> <p>Once I populate %domain_count with domain names and their counts, I try to get the top 50 values out like this:</p> <pre><code>my $num_top_domains = min(50, $total_count); my @top_domains_names = (); my @top_domains_counts = (); my $current_count = 0; while (($current_count &lt;= $num_top_domains) &amp;&amp; ((my $domain, my $count) = each %domain_count)) { push(@top_domains_names, $domain); push(@top_domains_counts, $count); $current_count++; } </code></pre> <p>Where my sorting function is:</p> <pre><code>sub compare_values { my ($key1, $key2) = @_; $domain_count{$key2} &lt;=&gt; $domain_count{$key1}; } </code></pre> <p>My understanding is that every invocation of "each %domain_count" invokes the sorting function. However, I am not certain that I am actually sorting the key-value pairs in order of descending numeric value and I am not certain that I am returning the top 50.</p> <p>Of course the script is quite a bit larger than what I have shown. I hope this is enough for analysis. All questions and advice are appreciated.</p> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload