It is possible to write a sort function that will produce any arbitrary order your heart desires. However, it might be difficult to write, and the resulting program might not be very efficient. A powerful technique is to transform the keys into something that can be sorted alphabetically. This is much easier to think about, and lets you take advantage of the special key-reduction optimizations that are built into the newer versions of DB_File.

If you know that the catalog numbers never have more than a certain number of digits, you can make them sortable by adding leading zeroes to numbers, like this:

...and so on. You can make this transformation with a simple statement like:
$key =~ s/(\d+)/sprintf "%03d", $1/eg;
Remember to keep a copy of the original key around so you can show it to your users, and they won't ever have to know what you've done.

If you don't have a guarantee about how many digits can be in a catalog number, there are clever solutions... You could, for instance, store the number of digits followed by the actual digits themselves.


In reply to Re: Re: Re: Question regarding DB_File by no_slogan
in thread Question regarding DB_File by Stamp_Guy

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.