473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sort and compare

Hi All,
I am trying to sort a column based on the numerical value but it seems to me that perl sorts based on first value of the column. For example, some of the data in my file is 23, 5467, 21, 64, 654, 342, 10
When i used the sort function on the array of these numbers, it sorted based on first numerical number and not based on total value. Like it sorted 10, 21, 23,342,5467, 64,654 but i am trying to get based on value like 5467,654,342,64,23,21,10.

Any help will be appreciated.
Thanks
Kumar
Oct 12 '08 #1
12 1520
numberwhun
3,509 Expert Mod 2GB
So, you wanted sorted in two ways, first, but the length of the number, and then a sub-sort to put them in order of each length, largest to smallest (reverse sort).

What code have you tried thus far? You need to show us what you have done before we can help you.

BTW, please put your code in code tags. An example is shown in the "Reply Guidelines" box to the right of the reply window.

Regards,

Jeff
Oct 12 '08 #2
KevinADC
4,059 Expert 2GB
to sort numerically you have to use the numeric comparison operator <=>:

Expand|Select|Wrap|Line Numbers
  1. @out = sort {$a <=> $b} @in;
otherwise perl sorts the numbers as ASCII strings, which is the default sort. Its covered in the sort functions documentation.

To reverse the order transpose $a and $b.
Oct 12 '08 #3
Thanks for your reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2. open (FH, "cluster4.info");
  3. while(<FH>)
  4. {
  5.     my $line = $_;
  6.     chomp $line;
  7.     if ($line=~/^#/)
  8.         {next;}
  9.     $line=~/.*\s(\d+).*=\s+(\d+)/;
  10.     my $cnum = $1;my $mem = $2;
  11.     $hash{$cnum} = $mem;
  12. }
  13.  
  14.     foreach $value (sort {$hash{$b} cmp $hash{$a} }
  15.            keys %hash)
  16. {
  17.     print "$value $hash{$value}\n";
  18. }
  19.  
Thanks
kumar
Oct 13 '08 #4
and here is my sample data
Expand|Select|Wrap|Line Numbers
  1. Cluster 1  Number of members =  23
  2. Cluster 2  Number of members =  285
  3. Cluster 3  Number of members =  4
  4. Cluster 4  Number of members =  28
  5. Cluster 5  Number of members =  1
  6. Cluster 6  Number of members =  24
  7. Cluster 7  Number of members =  54
  8. Cluster 8  Number of members =  246
  9. Cluster 9  Number of members =  1435
  10.  
Thanks
Kumar
Oct 13 '08 #5
KevinADC
4,059 Expert 2GB
Thanks for your reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2. open (FH, "cluster4.info");
  3. while(<FH>)
  4. {
  5.     my $line = $_;
  6.     chomp $line;
  7.     if ($line=~/^#/)
  8.         {next;}
  9.     $line=~/.*\s(\d+).*=\s+(\d+)/;
  10.     my $cnum = $1;my $mem = $2;
  11.     $hash{$cnum} = $mem;
  12. }
  13.  
  14.     foreach $value (sort {$hash{$b} cmp $hash{$a} }
  15.            keys %hash)
  16. {
  17.     print "$value $hash{$value}\n";
  18. }
  19.  
Thanks
kumar

Is there a question you have about your code?
Oct 13 '08 #6
Hi All,
Thanks for your help, I solved the problem of sorting and printing, only a small issue, while printing the data I want to put a new line(only once) when the value is less than 100 but when i am using the if condition its printing in the all the values which i don't want. Any help. I am posting my code, sample data and result output.
Expand|Select|Wrap|Line Numbers
  1. open (FH, "sample4.out") or die "Check the input file";
  2. my $cnt = 0;
  3. while (<FH>)
  4. {
  5.     $n1 = $_;
  6.     if($n1=~ /cluster t.(\d+) has (\d+)/)
  7.     {
  8.         $cid = $1;
  9.         $nc  = $2;
  10.         $hash{$cid} = $nc;
  11.         $cnt++;
  12.     }
  13. }
  14. print "#Total number of cluster = $cnt\n";
  15. close (FH);
  16.  
  17. $count = 1;
  18. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  19. {
  20.     print "$count Cluster $value : Number of members = $hash{$value}\n";if($hash{$value} >= 100){print "\n";}
  21.     $count++;
  22. }
  23.  
Expand|Select|Wrap|Line Numbers
  1. Sample data
  2. #Total number of cluster = 20
  3. Cluster 17  Number of members =  1
  4. Cluster 19  Number of members =  1
  5. Cluster 5  Number of members =  1
  6. Cluster 13  Number of members =  2
  7. Cluster 3  Number of members =  4
  8. Cluster 16  Number of members =  9
  9. Cluster 1  Number of members =  23
  10. Cluster 6  Number of members =  24
  11. Cluster 4  Number of members =  28
  12. Cluster 7  Number of members =  54
  13. Cluster 18  Number of members =  57
  14. Cluster 20  Number of members =  92
  15. Cluster 10  Number of members =  101
  16. Cluster 14  Number of members =  158
  17. Cluster 15  Number of members =  210
  18. Cluster 8  Number of members =  246
  19. Cluster 2  Number of members =  285
  20. Cluster 12  Number of members =  525
  21. Cluster 9  Number of members =  1435
  22. Cluster 11  Number of members =  5744
  23.  
Expand|Select|Wrap|Line Numbers
  1. RESULT
  2. #Total number of cluster = 20
  3. 1 Cluster 11 : Number of members = 5744
  4.  
  5. 2 Cluster 9 : Number of members = 1435
  6.  
  7. 3 Cluster 12 : Number of members = 525
  8.  
  9. 4 Cluster 2 : Number of members = 285
  10.  
  11. 5 Cluster 8 : Number of members = 246
  12.  
  13. 6 Cluster 15 : Number of members = 210
  14.  
  15. 7 Cluster 14 : Number of members = 158
  16.  
  17. 8 Cluster 10 : Number of members = 101
  18.  
  19. 9 Cluster 20 : Number of members = 92
  20. 10 Cluster 18 : Number of members = 57
  21. 11 Cluster 7 : Number of members = 54
  22. 12 Cluster 4 : Number of members = 28
  23. 13 Cluster 6 : Number of members = 24
  24. 14 Cluster 1 : Number of members = 23
  25. 15 Cluster 16 : Number of members = 9
  26. 16 Cluster 3 : Number of members = 4
  27. 17 Cluster 13 : Number of members = 2
  28. 18 Cluster 17 : Number of members = 1
  29. 19 Cluster 19 : Number of members = 1
  30. 20 Cluster 5 : Number of members = 1
  31.  
Thanks
Kumar
Oct 13 '08 #7
Icecrack
174 Expert 100+
Hi All,
Thanks for your help, I solved the problem of sorting and printing, only a small issue, while printing the data I want to put a new line(only once) when the value is less than 100 but when i am using the if condition its printing in the all the values which i don't want. Any help. I am posting my code, sample data and result output.

Thanks
Kumar


can you restate that i don't understand what your getting at.
Oct 13 '08 #8
KevinADC
4,059 Expert 2GB
I'm confused. The data you posted does not seem to be the data your script is working with.

Expand|Select|Wrap|Line Numbers
  1. if($n1=~ /cluster t.(\d+) has (\d+)/)
The data is not:

cluster t.nn has nn

where 'nn' is some integer
Oct 13 '08 #9
Sorry for the confusion,
actually what I am trying to do is, when the result is printed then, if the number of elements(last column values) are less than 100 then put a new line between the result.
so the result should look something like this:
Expand|Select|Wrap|Line Numbers
  1. #Total number of cluster = 17
  2.  
  3. 1 Cluster 12 : Number of members = 5309
  4. 2 Cluster 8 : Number of members = 1697
  5. 3 Cluster 17 : Number of members = 683
  6. 4 Cluster 1 : Number of members = 400
  7. 5 Cluster 7 : Number of members = 218
  8. 6 Cluster 5 : Number of members = 207
  9. 7 Cluster 16 : Number of members = 173
  10. 8 Cluster 2 : Number of members = 100
  11.  
  12. 9 Cluster 9 : Number of members = 79
  13. 10 Cluster 10 : Number of members = 54
  14. 11 Cluster 3 : Number of members = 53
  15. 12 Cluster 6 : Number of members = 16
  16. 13 Cluster 13 : Number of members = 4
  17. 14 Cluster 14 : Number of members = 3
  18. 15 Cluster 11 : Number of members = 2
  19. 16 Cluster 15 : Number of members = 1
  20. 17 Cluster 4 : Number of members = 1
  21.  
but with my code, its putting new line in every line after the number of elements are less than 100, so my results looks like this
Expand|Select|Wrap|Line Numbers
  1. #Total number of cluster = 17
  2.  
  3. 1 Cluster 12 : Number of members = 5309
  4. 2 Cluster 8 : Number of members = 1697
  5. 3 Cluster 17 : Number of members = 683
  6. 4 Cluster 1 : Number of members = 400
  7. 5 Cluster 7 : Number of members = 218
  8. 6 Cluster 5 : Number of members = 207
  9. 7 Cluster 16 : Number of members = 173
  10. 8 Cluster 2 : Number of members = 100
  11.  
  12. 9 Cluster 9 : Number of members = 79
  13.  
  14. 10 Cluster 10 : Number of members = 54
  15.  
  16. 11 Cluster 3 : Number of members = 53
  17.  
  18. 12 Cluster 6 : Number of members = 16
  19.  
  20. 13 Cluster 13 : Number of members = 4
  21.  
  22. 14 Cluster 14 : Number of members = 3
  23.  
  24. 15 Cluster 11 : Number of members = 2
  25.  
  26. 16 Cluster 15 : Number of members = 1
  27.  
  28. 17 Cluster 4 : Number of members = 1
  29.  
I know that i am printing the new line in the loop but i tried different combinations but neither worked.

Again sorry for the confusion.

Thanks
Kumar
Oct 13 '08 #10
Icecrack
174 Expert 100+
try this if this is what you are asking for?

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  3. {
  4. if ($hash{$value} >= 100){print "\n";}
  5.     print "$count Cluster $value : Number of members = $hash{$value}\n";
  6.     $count++;
  7.  
  8.  
  9. }
Oct 13 '08 #11
nithinpes
410 Expert 256MB
If you want to insert an extra newline only once before the values displayed are less than 100, use this approach.
Expand|Select|Wrap|Line Numbers
  1. $count = 1; 
  2. my @high = grep($hash{$_} >= 100, keys %hash);
  3. $hash{$high[$#high]} = $hash{$high[$#high]}. "\n"; #append extra newline
  4.  
  5. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash) 
  6.     print "$count Cluster $value : Number of members = $hash{$value}\n"; 
  7.     $count++; 
  8.  
Oct 13 '08 #12
Icecrack
174 Expert 100+
Ok i think i got what you mean now,

you want the first line that is below or equal to 100 to print a new line before it and thats it.

try:
Expand|Select|Wrap|Line Numbers
  1. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  2. {
  3. $flag=0;
  4. if (($hash{$value} >= 100) && ($flag == 0)
  5. {
  6. print "\n"; $flag=1;
  7. }
  8.     print "$count Cluster $value : Number of members = $hash{$value}\n";
  9.     $count++;
  10. }
Oct 13 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
10
by: Paul Schneider | last post by:
I want to sort a class derived from std::vector with STL sort.: template<typename T, typename fitParaType, typename fitResType> class Manipulator{ // shouldn't I now be able to access private...
4
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
9
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
10
by: eiji | last post by:
Hi folks, I have a problem compiling this under VC6! Maybe someone has some minutes to look at this: template<class T> class Compare { public: Compare(){}; virtual ~Compare(){};
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.