Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting text databases with perl's sort function

Status
Not open for further replies.

Quilmes

Programmer
Aug 13, 2000
12
0
0
AU
Hi,

I have a flat file text database with fields separated by pipes.

I have to be able to sort any text field in descending or ascending order. I have tried to use the perl sort function in combination with building up a hash as I have seen in other posts.

I can get it to work if I make the hash in the format:
(val_to_sort_by => row)
i.e. the hash key is the value of the field I am sorting by and the hash value is the entire row.

However this does not suit my purpose because there are case in which the field I am sorting by has duplicate values on various rows. In this case, using the field value as the hash key is not possible because the value is not unique.

I would like to use the perl sorting function as it is neat and quick, but I am thinking I will have to write brute force sorting function myself. Can anyone advise on how to sort such files?

Thanks in advance

Joe Niland
mailto:joe.niland@meniscus.com.au [sig][/sig]
 
You could arrange it so that the hash is always built using unique data as the key value -- but that the field you want to sort by is the *first* field in the data..

Then, instead of calling

sort keys (%hasha)

call

sort values (%hasha)

[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top