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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Question

Status
Not open for further replies.

mike101

Programmer
Jul 20, 2001
169
US
Hey everyone. I have a database that I make into an array and sort in this following code.

open (ORGDB,&quot;<$database&quot;);
@old_list=<ORGDB>;
@ODB=sort(@old_list);
close (ORGDB);

Where $database points to the database.
A sample database is

1/07/01|Test|Whatever
1/04/01|Test2|Whatever

It currently sorts by the first set which would be the dates. Say I want to sort by the column of Test and Test2 how do I change my code to sort by that? Thanks.
 
Actually, currently it is sorting by the whole line
on a lexical way.

You must pass a hand-made function to sort, that splits
and compares your field.
For instance, sort by second field (array pos 1):

replace &quot;@ODB=sort(@old_list);&quot; by:

@ODB=sort {@aa=split /\|/, $a;
@bb=split /\|/, $b;
$aa[1] cmp $bb[1] } @old_list;

see also: perldoc -tf sort

cya

--
pkiller


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top