in reply to Database Structure/Sorting/Displaying

Well, I'll just address your sorting question/issue--- you can sort on the catalogue number by dividing each one into 3 components: leading non-digits (if any), digits, trailing non-digits (if any) ... and then defining the sort routine to sort on these components. Here's an off the cuff Schwartzian Transform applied to the data:

#!/usr/bin/perl -w use strict; my @list = qw/715b E9 715a 614 715 C18 1 4 12a 12/; my @sorted = map{$_->[0]} sort{$a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] || $a->[3] cmp $b->[3]} map{[$_,/(\D*)(\d+)(\D*)/]} @list; print join("\n", @sorted),"\n";

Knowing how to sort on catalogue numbers should relieve you of the need to use a special sort N field in the data. Having said that, you might also want to look into the DB_File dbm system which has a BTREE mode for keeping entries in sorted order (you can provide a comparison routine), and it has facilities for matching partial keys which can be used to code for retrieving ranges.

Replies are listed 'Best First'.
Re: Re: Database Structure/Sorting/Displaying
by Stamp_Guy (Monk) on Jul 04, 2001 at 23:41 UTC
    Hmmm. Ok, I didn't realize that was possible (but I shoulda figured... it's Perl :-) ). Here's a question though: if I don't have DB_File, and I want to display a range of numbers, say 614 - C18, how would I be able to do it without the sort field?

      Since you can sort on catalogue number, you can loop over the sorted keys and skip the ones not in the range:

      foreach my $key (@sorted) { next unless $key eq '614' .. $key eq 'C18'; print "$key: $db{$key} \n"; }

      Assuming the hash is %db. I suspect you were doing something similar but using the sort N field? The downfall is that you need to get the whole list of keys into memory --- with DB_File that could be avoided.