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

Record Sorting with split 1

Status
Not open for further replies.

grapnell

Programmer
Joined
Sep 12, 2006
Messages
1
Location
US
I am trying to write a function to sort multiple columns of data in a cgi script, however, I am not having any success.

the function I am using is as follows:
Code:
@ass_temp = sort {
    (split '\|', $a, 1)[2] cmp
    (split '\|', $b, 1)[2]
  } @assignment;
@assignment = @ass_temp;
and the data array looks like the following, with each line representing one element of the array in @assignment...
Code:
31|accounting|8/18/2006|c|10113|robert trigg||2|
32|accounting|8/21/2006|c|19654|13007||2|
33|accounting|8/21/2006|c|2047|steve||2|
35|accounting|8/21/2006|c|9000|macray||2|
36|accounting|8/21/2006|c|23892|calvin|rush|2|
37|accounting|8/21/2006|c|30006|JAMES||2|
38|accounting|8/22/2006|c|11184|sonny|rush|2|

I am trying to be able to sort by the first or fifth column, among others, but the code I have written above has no effect on the array.

Not sure what exactly what is wrong, or right for that matter.
ANy help appreciated,

Jeff
 
This would sort on the fifth column

Code:
my @ass_temp = map {$_->[0]}
               sort{$a->[5] <=> $b->[5]}
               map {chomp; [$_,split(/\|/)]} <DATA>;
print "$_\n" for @ass_temp;
__DATA__
31|accounting|8/18/2006|c|10113|robert trigg||2|
32|accounting|8/21/2006|c|19654|13007||2|
33|accounting|8/21/2006|c|2047|steve||2|
35|accounting|8/21/2006|c|9000|macray||2|
36|accounting|8/21/2006|c|23892|calvin|rush|2|
37|accounting|8/21/2006|c|30006|JAMES||2|
38|accounting|8/22/2006|c|11184|sonny|rush|2|

I used [5] instead of [4] for the fifth column because [0] is a copy of the original lines of the data which gets put into @ass_temp once the data is sorted.

 
nice one ishnid

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top