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
12 1520
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
to sort numerically you have to use the numeric comparison operator <=>: - @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.
Thanks for your reply,
here is my code -
my %hash;
-
open (FH, "cluster4.info");
-
while(<FH>)
-
{
-
my $line = $_;
-
chomp $line;
-
if ($line=~/^#/)
-
{next;}
-
$line=~/.*\s(\d+).*=\s+(\d+)/;
-
my $cnum = $1;my $mem = $2;
-
$hash{$cnum} = $mem;
-
}
-
-
foreach $value (sort {$hash{$b} cmp $hash{$a} }
-
keys %hash)
-
{
-
print "$value $hash{$value}\n";
-
}
-
Thanks
kumar
and here is my sample data -
Cluster 1 Number of members = 23
-
Cluster 2 Number of members = 285
-
Cluster 3 Number of members = 4
-
Cluster 4 Number of members = 28
-
Cluster 5 Number of members = 1
-
Cluster 6 Number of members = 24
-
Cluster 7 Number of members = 54
-
Cluster 8 Number of members = 246
-
Cluster 9 Number of members = 1435
-
Thanks
Kumar
Thanks for your reply,
here is my code -
my %hash;
-
open (FH, "cluster4.info");
-
while(<FH>)
-
{
-
my $line = $_;
-
chomp $line;
-
if ($line=~/^#/)
-
{next;}
-
$line=~/.*\s(\d+).*=\s+(\d+)/;
-
my $cnum = $1;my $mem = $2;
-
$hash{$cnum} = $mem;
-
}
-
-
foreach $value (sort {$hash{$b} cmp $hash{$a} }
-
keys %hash)
-
{
-
print "$value $hash{$value}\n";
-
}
-
Thanks
kumar
Is there a question you have about your code?
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. -
open (FH, "sample4.out") or die "Check the input file";
-
my $cnt = 0;
-
while (<FH>)
-
{
-
$n1 = $_;
-
if($n1=~ /cluster t.(\d+) has (\d+)/)
-
{
-
$cid = $1;
-
$nc = $2;
-
$hash{$cid} = $nc;
-
$cnt++;
-
}
-
}
-
print "#Total number of cluster = $cnt\n";
-
close (FH);
-
-
$count = 1;
-
foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
-
{
-
print "$count Cluster $value : Number of members = $hash{$value}\n";if($hash{$value} >= 100){print "\n";}
-
$count++;
-
}
-
-
Sample data
-
#Total number of cluster = 20
-
Cluster 17 Number of members = 1
-
Cluster 19 Number of members = 1
-
Cluster 5 Number of members = 1
-
Cluster 13 Number of members = 2
-
Cluster 3 Number of members = 4
-
Cluster 16 Number of members = 9
-
Cluster 1 Number of members = 23
-
Cluster 6 Number of members = 24
-
Cluster 4 Number of members = 28
-
Cluster 7 Number of members = 54
-
Cluster 18 Number of members = 57
-
Cluster 20 Number of members = 92
-
Cluster 10 Number of members = 101
-
Cluster 14 Number of members = 158
-
Cluster 15 Number of members = 210
-
Cluster 8 Number of members = 246
-
Cluster 2 Number of members = 285
-
Cluster 12 Number of members = 525
-
Cluster 9 Number of members = 1435
-
Cluster 11 Number of members = 5744
-
-
RESULT
-
#Total number of cluster = 20
-
1 Cluster 11 : Number of members = 5744
-
-
2 Cluster 9 : Number of members = 1435
-
-
3 Cluster 12 : Number of members = 525
-
-
4 Cluster 2 : Number of members = 285
-
-
5 Cluster 8 : Number of members = 246
-
-
6 Cluster 15 : Number of members = 210
-
-
7 Cluster 14 : Number of members = 158
-
-
8 Cluster 10 : Number of members = 101
-
-
9 Cluster 20 : Number of members = 92
-
10 Cluster 18 : Number of members = 57
-
11 Cluster 7 : Number of members = 54
-
12 Cluster 4 : Number of members = 28
-
13 Cluster 6 : Number of members = 24
-
14 Cluster 1 : Number of members = 23
-
15 Cluster 16 : Number of members = 9
-
16 Cluster 3 : Number of members = 4
-
17 Cluster 13 : Number of members = 2
-
18 Cluster 17 : Number of members = 1
-
19 Cluster 19 : Number of members = 1
-
20 Cluster 5 : Number of members = 1
-
Thanks
Kumar
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.
I'm confused. The data you posted does not seem to be the data your script is working with. - if($n1=~ /cluster t.(\d+) has (\d+)/)
The data is not:
cluster t.nn has nn
where 'nn' is some integer
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: -
#Total number of cluster = 17
-
-
1 Cluster 12 : Number of members = 5309
-
2 Cluster 8 : Number of members = 1697
-
3 Cluster 17 : Number of members = 683
-
4 Cluster 1 : Number of members = 400
-
5 Cluster 7 : Number of members = 218
-
6 Cluster 5 : Number of members = 207
-
7 Cluster 16 : Number of members = 173
-
8 Cluster 2 : Number of members = 100
-
-
9 Cluster 9 : Number of members = 79
-
10 Cluster 10 : Number of members = 54
-
11 Cluster 3 : Number of members = 53
-
12 Cluster 6 : Number of members = 16
-
13 Cluster 13 : Number of members = 4
-
14 Cluster 14 : Number of members = 3
-
15 Cluster 11 : Number of members = 2
-
16 Cluster 15 : Number of members = 1
-
17 Cluster 4 : Number of members = 1
-
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 -
#Total number of cluster = 17
-
-
1 Cluster 12 : Number of members = 5309
-
2 Cluster 8 : Number of members = 1697
-
3 Cluster 17 : Number of members = 683
-
4 Cluster 1 : Number of members = 400
-
5 Cluster 7 : Number of members = 218
-
6 Cluster 5 : Number of members = 207
-
7 Cluster 16 : Number of members = 173
-
8 Cluster 2 : Number of members = 100
-
-
9 Cluster 9 : Number of members = 79
-
-
10 Cluster 10 : Number of members = 54
-
-
11 Cluster 3 : Number of members = 53
-
-
12 Cluster 6 : Number of members = 16
-
-
13 Cluster 13 : Number of members = 4
-
-
14 Cluster 14 : Number of members = 3
-
-
15 Cluster 11 : Number of members = 2
-
-
16 Cluster 15 : Number of members = 1
-
-
17 Cluster 4 : Number of members = 1
-
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
try this if this is what you are asking for? -
-
foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
-
{
-
if ($hash{$value} >= 100){print "\n";}
-
print "$count Cluster $value : Number of members = $hash{$value}\n";
-
$count++;
-
-
-
}
If you want to insert an extra newline only once before the values displayed are less than 100, use this approach. -
$count = 1;
-
my @high = grep($hash{$_} >= 100, keys %hash);
-
$hash{$high[$#high]} = $hash{$high[$#high]}. "\n"; #append extra newline
-
-
foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
-
{
-
print "$count Cluster $value : Number of members = $hash{$value}\n";
-
$count++;
-
}
-
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: - foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
-
{
-
$flag=0;
-
if (($hash{$value} >= 100) && ($flag == 0)
-
{
-
print "\n"; $flag=1;
-
}
-
print "$count Cluster $value : Number of members = $hash{$value}\n";
-
$count++;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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(){};
|
by: rkk |
last post by:
Hi,
I have written a generic mergesort program which is as below:
---------------------------------------------------------
mergesort.h
-----------------------
void
MergeSort(void...
|
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.
...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |