How do I do filtering in Perl (keep sort order and sort again by another means)?
Ok, I have a list of items and associated values that were pre-sorted according to value. Some values are the same so I want to sort those by name as well. Here's what I mean:
Desired Output: Code:
pink|3.3 Code:
Code:
$VAR1 = 'pink|3.3'; |
can you not use sort?
it would much easier. i.e. not perl. |
You can do this type of thing by writing a custom sort subroutine. Here is a quick stab. It works, but there are probably more efficient ways to do it:
Code:
#!/usr/bin/perl Edit: If you want the original array itself sorted just do Code:
@arry_data = sort my_way @arr_data; |
A second way which is a lot more complicated to look at, but perhaps more efficient:
Code:
#!/usr/bin/perl |
the true way of unix would be:
/bin/sort -t\| -k2,3n -k1,2 < list |
Whoa - a perl thread that bigearsbilly refuses to answer.
What's the world coming to ???. |
ho ho!
as a true perl (zeal|big)ot and officianado even I would go for the path of least typing on this one. ;) unless of course maybe the individual is unfortunate enough to be using perl on windows. |
Quote:
In any case, here is a fixed sort incantation: Code:
sort -t\| -k2,3nr -k1,2 < unsorted |
1. doh!
sort -k2,3rn -k1,2 2. I don't. Just opining :) Still it is best to use the power at your fingertips if you can. |
bigearsbilly - I can't use sort (ie. not perl) because this is to be a part of a larger perl script I'm writing in which it all should be self-contained since I'm sorting data that is already loaded into the variables. I was trying to right a sort function for including in the script I'm working on.
Telemachos - Your Schwartzian transform code example is what I'll go with. Thank you. :) |
All times are GMT -5. The time now is 01:29 AM. |