Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: changing the printing order of a db

by jsprat (Curate)
on Jul 19, 2003 at 02:13 UTC ( #275804=note: print w/replies, xml ) Need Help??


in reply to changing the printing order of a db

The default order of $DB_BTREE is lexical. If you use a date/time stamp for the key, keys will return the keys in sorted order automatically and you won't need a custom sort order - ie:

$tied_hash{20030718190355} = $value;

Another approach using $DB_RECNO can be found at Re: Re: Re: Re: Re: Re: DB_File, not saving.

HTH...

Replies are listed 'Best First'.
Re: Re: changing the printing order of a db
by Anonymous Monk on Jul 19, 2003 at 03:54 UTC
    Ok, I tried your idea and am using $hash{localtime()} = $info but it prints data in a miscellaneus order. Any other possibilities?
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://275804]
help
Chatterbox?
[erix]: are there any regex-engines that do not do multiline?
[Lady_Aleena]: erix, grep doesn't.
[Lady_Aleena]: Linux grep that is...
[Lady_Aleena]: $ grep --include=*.p[lm] -Ern 'get_(array|hash| data)' does not do multiline search.
[Discipulus]: focus on Perl LA; you can already search multiline patterns
[erix]: grep is a program, not a regex-engine
[Tanktalus]: perldigious: I'm 20 years in, and I don't want to move ;)
[Lady_Aleena]: erix, right, it searches files for a pattern. pcregrep is supposed to do the same thing with the addition of being able to search multiline.
[erix]: ah, pcregrep is a stanalone program?
[Lady_Aleena]: erix, yes.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2017-05-23 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?