Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
localtime in scalar context returns a string something like "Fri Jul 18 23:35:45 2003" which won't sort cleanly. Build the key like "YYYYMMDDHHMMSS" and the keys will be sorted on disk.

#!/usr/bin/perl use strict; use warnings; use DB_File; unlink 'db'; # to prevent dupes if this is run more than once tie my %hash, 'DB_File', 'db', O_CREAT|O_RDWR, 0644, $DB_BTREE; print "Building \%hash...\n"; $|++; for my $count (1 .. 50) { my @time = localtime; my $key = $time[5] + 1900; $key .= sprintf "%02d", $time[$_] for reverse 0 .. 4; print "$count... " if $count % 10 == 0; $hash{$key} = "Message number $count"; sleep 1; } print "Output:\n"; print "$_: $hash{$_}\n" for keys %hash;

Check the output, you'll see what I mean. The keys come out lexically sorted - the messages go from Message #1 to #50.

Remember that you can have duplicate keys with $DB_BTREE. If two values are inserted with the same time stamp, there's no guarantee which way they will be retrieved (which is why I suggested $DB_RECNO).

HTH


In reply to Re: Re: Re: changing the printing order of a db by jsprat
in thread changing the printing order of a db by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found