For hashes keyed by integer values (positive or negative) represented by 32 bits, returns a list containing the minimum and maximum key values found. Example included in code.
#!/usr/local/bin/perl -w # # A very speedy min/max key finder for hashes keyed by integer values +. # # Requires Perl 5.004 or later, due to the hv_iterkeysv macro. # # Copyright 2000,2001(c) J.C.Wren jcwren@jcwren.com # A production of Twitching Monk Software # No rights reserved, use as you see fit. I'd like to know about it, + though, just for kicks. # use strict; { my %hash = ( 8 => 'eight', 22 => 'twenty-two', 99 => 'ninety-nine', 4 => 'four', 0 => 'zero', -66 => 'negative sixty-six', ); my ($min, $max) = minmaxhash (\%hash); printf ("min key=%d, min key value=%s\n", $min, $hash {$min}); printf ("max key=%d, max key value=%s\n", $max, $hash {$max}); } use Inline C => <<'END_OF_C_CODE'; void minmaxhash (SV* hash_ref) { Inline_Stack_Vars; HV* hash; HE* hash_entry; int i; int num_keys; int temp, min, max; if (!SvROK(hash_ref)) croak("hash_ref is not a reference"); hash = (HV*)SvRV(hash_ref); num_keys = hv_iterinit(hash); for (min = INT_MAX, max = INT_MIN, i = 0; i < num_keys; i++) { /* ** This is a bug: I should be able to eliminate hash_entry ** as a temporary varaible. On my system, the statement ** "if ((temp = SvIV(hv_iterkeysv(hv_iternext(hash)))) > max)" ** causes a segfault. */ hash_entry = hv_iternext(hash); if ((temp = SvIV(hv_iterkeysv(hash_entry))) > max) max = temp; if (temp < min) min = temp; } Inline_Stack_Reset; Inline_Stack_Push(sv_2mortal(newSViv(min))); Inline_Stack_Push(sv_2mortal(newSViv(max))); Inline_Stack_Done; } END_OF_C_CODE

In reply to Speedy min/max integer key finder for hashes by jcwren

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.