Here is one shot: while ( <DATA> ) { chomp; # remove \n push(@dbfile, $_); } printf "Sort numerically:\n";
foreach my $MyData (sort {$a->[1] <=> $b->[1]} map{[$_, /^(\d+)/]} @dbfile) { printf "%-15s%5d: %-s\n", ' ', $MyData->[1], $MyData->[0]; } printf "Sort Alphabetically:\n"; foreach my $MyData (sort {$a->[1] cmp $b->[1]} map{[$_, /^\d+,([^,]+)/]} @dbfile) { printf "%20s: %-s\n", $MyData->[1], $MyData->[0]; } printf "Sort Date/Time:\n"; foreach my $MyData (sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]} map{[$_, /^\d+,[^,]+,(\d+)\-(\d+)/]} @dbfile) { printf "%20s: %-s\n", $MyData->[1] . '-' . $MyData->[2], $MyData->[0]; } Output: Sort numerically: 4: 4,grapes,20021118-1921 16: 16,apples,20021118-1725 22: 22,bananas,20021118-1648 Sort Alphabetically: apples: 16,apples,20021118-1725 bananas: 22,bananas,20021118-1648 grapes: 4,grapes,20021118-1921 Sort Date/Time: 20021118-1648: 22,bananas,20021118-1648 20021118-1725: 16,apples,20021118-1725 20021118-1921: 4,grapes,20021118-1921 Wags ;) -----Original Message----- From: Gavin Laking [mailto:[EMAIL PROTECTED]] Sent: Monday, November 18, 2002 13:53 To: [EMAIL PROTECTED] Subject: Flatfile database sorting in Perl ********************************************************** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. **************************************************************** -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi folks, One of my scripts uses a home-brewed flatfile database to store information, and I've run up against a problem when I wish to sort columns of data. An example of the database file: (items,fruit,date) 16,apples,20021118-1725 22,bananas,20021118-1648 4,grapes,20021118-1921 To open and place the items into an array, I do this (formatted so that some mail client doesn't make it *really* unreadable :-P ) : open ('FILE',"$databaseFile") || die; @dbfile = <FILE>; for ($a = 0; $a < @dbfile; $a++) {( $items[$a], $fruit[$a], $date[$a], $chop)=split(/,/,$dbfile[$a]);} With a little pretty printing can show us the database in 3D. I'm not going to include the code for pretty printing here as it is unimportant, but the output I think is: [0]16,[0]apples,[0]20021118-1725 [1]22,[1]bananas,[1]20021118-1648 [2]4,[2]grapes,[2]20021118-1921 Now my problem is that I can sort the database by 'items', both ascending and descending order: (ascending) @dboutfile = sort {($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0]} @dbfile; (descending) @dboutfile = sort {($b =~ /(\d+)/)[0] <=> ($a =~ /(\d+)/)[0]} @dbfile; But I can't seem to sort the database by any of the other fields. I've tried incrementing the '0's and changing '\d+' to '\w+' but I can't get it into reverse alphabetical or chronological order or any other sort type other than ascending or descending 'items' order. Does anybody know what I'm waffling about, and can solve this problem, or am I really blessed with programmers illiteracy? Thanks all, GL - -- Gavin Laking - Web Development Daemon http://www.gavinlaking.co.uk - -- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) iD8DBQE92WE1aMak7Ylvm3ARAu7NAKDusbcH57Uw0QfN8mdECRwMaAFXrACbBkPv w1FOeSr0d4tGI+tB68DY/Ic= =V5tW -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] ********************************************************** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. **************************************************************** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]