Sort after 2. column in array in Perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort after 2. column in array in Perl
# 1  
Old 03-25-2010
Sort after 2. column in array in Perl

Hey

How do I sort an array numerically after the second column?
My values are integers like 1, 2, 3, 4... and they are not unique, so I can't just reverse my hash and sort by keys.
I wanna sort my file/array so that I get the lines with the highest value in the top - that is descending.
This is the part of my code that causes problems:

Code:
 
while (my ($accno, $count) = each %ACCcount) {    
   push ( @ACCcount, "$accno");
   push ( @ACCcount, " $count\n" );  #The space is just included because of the layout when I print the array to a file. But maybe I should push the space and newline in as separate elements and sort by column 3 instead. 
}
 
@sorted = sort { $b->[1] <=> $a->[1] } @ACCcount; 
# I also tried the following(but get the exact same warning) eventhough the order of the keys doesn't matter. They of cause just need to get sorted along with the values: 
# @sorted = sort { $b->[1] <=> $a->[1] || $b->[0] cmp $a->[0]} @ACCcount;

When I try running this I get this warning:

Can't use string ("D25304") as an ARRAY ref while "strict refs" in use at ./program line 66, <ACCIN> line 9861.

D25304 is one of the keys.

Any ideas to how to solve this problem or what is wrong with my code?
# 2  
Old 04-12-2010
Show us the data which the %ACCcount hash is having ?
# 3  
Old 04-12-2010
Bug Perl Sorting on the second Field of array elements.

Code:
my @ACCcount = qw~123 456 7898 D1234 NN781 111 900 ZMZ ZAZ~
my @sorted = sort { substr( $a, 1, 1 ) cmp substr( $b, 1, 1 ) } @ACCcount;
print map "$_\n", @sorted;

1. you are getting the "Can't use Foo as an array ref" because "@ACCcount" is an array, not an arrayref. when you say $a->[1] you are pointing at a scalar arrayref that does not exist.

2. You Can't use the spaceship operator ( <=> ) to sort, unless all the arguments are being sorted are numeric, so you are stuck with cmp ( or a custom subroutine ).

3. Since you did not provide data I made some up, but I think this is what you want. You are sorting on the second SLOT ( slot[1] ) of each element of the array. so the first to things that are being considered for sort in the example are "2" and "5" Note: they are just considered first, that is not their final position. Can you see why?

4. Here is the output, with the data i made up, except that I bolded what determined the sort order.

Code:
900
D1234
111
123
456
7898
ZAZ
ZMZ
NN781

Hope that helps.

P.S. If you want descending reverse the positions of $a and $b.

Last edited by deindorfer; 04-12-2010 at 10:09 PM.. Reason: Addt'l Comment
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sort to sort numerical column

How to sort the following output based on lowest to highest BE? The following sort does not work. $ sort -t. -k1,1n -k2,2n bfd.txt BE31.116 0s 0s DOWN DAMP BE31.116 0s 0s DOWN DAMP BE31.117 0s 0s ... (7 Replies)
Discussion started by: sand1234
7 Replies

2. UNIX for Dummies Questions & Answers

Sort command in one column and not effect to another column

If my data is numerical : 1 = 101 2 = 102 3 = 104 4 = 104 7 = 103 8 = 103 9 = 105 I need the result like below: 1 = 101 2 = 102 3 = 103 4 = 103 7 = 104 8 = 104 9 = 105 (4 Replies)
Discussion started by: GeodusT
4 Replies

3. Shell Programming and Scripting

Sort flat file by 3rd column in perl

Hello Guys I want to sort a flat file by the third column (numeric ) and store it in some other name I/P 9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00 9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00... (12 Replies)
Discussion started by: Pratik4891
12 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

6. Shell Programming and Scripting

PERL : Sort substring occurrences in array of strings

Hi, My developer is on vacation and I am not sure if there is something which is easier for this. I have an array of strings. Each string in the array has "%" characters in it. I have to get the string(s) which have the least number of "%" in them. I know how I can get occurrences : ... (7 Replies)
Discussion started by: sinpeak
7 Replies

7. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

9. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

10. Shell Programming and Scripting

sort() array of strings in perl

I have a perl script with an array of clients. @arr = ("The ABC Corp", "AA Corp.", "BB Corp"); If I run @a = sort (@arr); I will get @a = ("AA Corp", "BB Corp", "The ABC Corp"); but I want @a = ("AA Corp, "The ABC Corp", "BB Corp"); How do I sort array without changing... (2 Replies)
Discussion started by: photon
2 Replies
Login or Register to Ask a Question