in reply to Re: Complex hash sorting
in thread Complex hash sorting

The problem with some of the other solutions is, this isn't a static hash I'm trying to sort. This is a tied hash to an SDBM database, so just using an array isn't an option as the program has been up and running with data stored for quite some time already.

Your first method seems to be easier to understand/follow, though I've never seen things done like this before...so I'll try going with that. Can you explain what/why $criteria is and why it's 3? And after it's in an array, how would you split everything back into separate variables for printing?

This is an example of my actual coding

tie %db, "DB_File", "$db", O_CREAT | O_RDWR, 0644, $DB_BTREE or die "Cannot open file 'db': $!\n"; print "<table>"; foreach (keys %db) { my ($count, $ip, $time) = split(/\|\|/, $db{$_}); print "<td>$count</td><td>$ip</td><td>$time</td>"; } </table>
There are four values (including the key itself), I want to make three of these sortable: the key, $count and $time). My method of doing this is calling a url_param defining the choice of sort. Ie. script.cgi?sort_by=count or script.cgi?sort_by=key and depending on what they chose, a different hash sort method will be used.

Thanks for your time.

Replies are listed 'Best First'.
Re: Re: Re: Complex hash sorting
by flyingmoose (Priest) on Feb 02, 2004 at 19:16 UTC
    Just an observation, but if tied hashes are slow, and sorting hashes based on complex operations are slow, and regexes are kind of slow... this code must really break down with a large number of records.

    This seems to be a classic case where taking the time to load the records into a (SQL) database and then query the database would produce cleaner code with (potentially) a speed savings of (who knows) 10x? Use temp tables or temp views if you must.