Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Sorting on a particular field from a Flat File db??

by Russ (Deacon)
on Nov 04, 2000 at 21:11 UTC ( [id://40006]=note: print w/replies, xml ) Need Help??


in reply to Sorting on a particular field from a Flat File db??

my @SortedKeys = sort {$field{$a} cmp $field{$b}} keys %fields;
We are creating our own sort routine, where we can use the keys in the %fields hash, but compare the values belonging to those keys to perform the sort.

This will cause a lot of hash lookups, but my benchmarks show that hash lookups are not expensive enough to warrant a Schwartzian Transform.

<Update> Oops, didn't read the question</Update>

my @SplitEntries = map{[split( '|', $_)]} @Entries; foreach $Lineref (sort {$a->[8] cmp $b->[8]} @SplitEntries) { my @Fields = @$Lineref; # This will be in sorted order, by Artist Name # $Fields[0] is CassID, $Fields[1] is CDID, etc }
New explanation: We create arrays of fields from each line, storing them in @SplitEntries (each entry in @SplitEntries is an arrayref holding the fields).

Now, we can sort the entries on Artist Name.

This is not the most CPU-efficient way to do this, either. If your music database is really big, you may want to get the Artist Name for each entry and the array index of its line. Then, sort the names and use the indices to get back to the real array.

my @SplitEntries = map{[split( '|', $_)]} @Entries; my @Names = map{[$_, @SplitEntries[$_]]} @SplitEntries; my @SortedIndices = map {$_->[0]} sort {$a->[1] cmp $b->[1]} @Names; foreach $lineref (@SplitEntries{@SortedIndices}){ my @Fields = @$Lineref; ... }
Even better, you should consider using DBI and let it manage your sorting needs. ;-)

Russ
Brainbench 'Most Valuable Professional' for Perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://40006]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found