Sorting a Multi-dimenional Array

J

jason_mandal

Hi,
My data consists of something like this

Column1 Column 2 Column 3 Column 4
Name1 123e-10 34e-5 -1.0567e-8
Name2 23e-10 4e-5 1.0567e-8
Name3 13e-10 3e-5 -1.0567e-9
Name4 12e-10 33e-5 .0011
Name5 123e-10 34e-5 -1.0567e-8


I need to sort this data with descending order on column 4.NOTE the
values are exponential and include both positive and negative
exponentials!!!
Also I do not want to lose any rows with same value in column 4.

Thanks in advance for your time

Jason
 
T

Tore Aursand

My data consists of something like this
"Something"?

Column1 Column 2 Column 3 Column 4
Name1 123e-10 34e-5 -1.0567e-8
Name2 23e-10 4e-5 1.0567e-8
Name3 13e-10 3e-5 -1.0567e-9
Name4 12e-10 33e-5 .0011
Name5 123e-10 34e-5 -1.0567e-8

I need to sort this data with descending order on column 4.NOTE the
values are exponential and include both positive and negative
exponentials!!!
Also I do not want to lose any rows with same value in column 4.

What have you tried so far? Have you read 'perldoc -q sort'?
 
J

jason_mandal

With something I meant nothing. The data I am trying to sort has been
listed. I tried to seek help from perldoc, and I am able to sort for
integers, but when the numbers are exponential I have no clear idea of
how to use the sort function.
Use of <=> and cmp does not help as they do not understand exponential
values. Correct me if I am wrong

Thanks Tore
 
J

Jürgen Exner

(e-mail address removed) wrote:

Sorry, but I must have a bad day. I can't even remotely guess what you are
talking about.
With something I meant nothing.

What do you mean with this sentence? You don't reference the word
'something' anywhere else in your post. Why are you saying you mean
'nothing' when saying 'something'? BTW: wouldn't it be easier to just say
'nothing' in the first place?
The data I am trying to sort has been
listed.

Where? I can't seem to see it in this post.
I tried to seek help from perldoc,
Good.

and I am able to sort for integers,

Seems you had some success
but when the numbers are exponential I have no clear idea of
how to use the sort function.
Use of <=> and cmp does not help as they do not understand exponential
values. Correct me if I am wrong

Please define "exponential number". Are you referring to scientific
notations? That is only a notation and perl (i.e. the interpreter) doesn't
care how you write a number. Internally it is always just a value.
Could you please provide a minimal sample program that demonstrates why you
believe that cmp or <=> don't "understand" some numbers?

BTW: what do you mean by "understand"?

jue
 
I

ioneabu

Hi,
My data consists of something like this

Column1 Column 2 Column 3 Column 4
Name1 123e-10 34e-5 -1.0567e-8
Name2 23e-10 4e-5 1.0567e-8
Name3 13e-10 3e-5 -1.0567e-9
Name4 12e-10 33e-5 .0011
Name5 123e-10 34e-5 -1.0567e-8


I need to sort this data with descending order on column 4.NOTE the
values are exponential and include both positive and negative
exponentials!!!
Also I do not want to lose any rows with same value in column 4.

Thanks in advance for your time

Jason

I don't know if my logic and output are correct, but I think you need
something along these lines:

#!/usr/bin/perl

use strict;
use warnings;

open my $file, '<', 'test.dat' or die "error: $!\n";
my @data = <$file>;
close $file;
my @data2;
use Math::BigFloat;
for (@data)
{
if (/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\.*/)
{
my $x = Math::BigFloat->new($4);
push @data2, [$1,$2,$3,$x]
}
}
sort {$a->[3]->bcmp($b->[3])} @data2 if @data2;
print "$_->[0]\t$_->[1]\t$_->[2]\t",$_->[3]->bsstr(),"\n" for @data2;


good luck!

wana
 
S

Sherm Pendley

With something I meant nothing. The data I am trying to sort has been
listed. I tried to seek help from perldoc, and I am able to sort for
integers, but when the numbers are exponential I have no clear idea of
how to use the sort function.
Use of <=> and cmp does not help as they do not understand exponential
values. Correct me if I am wrong

Sorry - you are wrong. For example, this script:

#!/usr/bin/perl

use Data::Dumper;
use warnings;
use strict;

my @numbers = (
[ 'Name1', 123e-10, 34e-5, -1.0567e-8 ],
[ 'Name2', 23e-10, 4e-5, 1.0567e-8 ],
[ 'Name3', 13e-10, 3e-5, -1.0567-9 ],
[ 'Name4', 12e-10, 33e-5, .0011 ],
[ 'Name5', 123e-10, 34e-5, -1.0567e-8 ],
);

my @sorted = sort { $b->[3] <=> $a->[3] } @numbers;

print Dumper(\@sorted);

Produces this output:

$VAR1 = [
[
'Name4',
'1.2e-09',
'0.00033',
'0.0011'
],
[
'Name2',
'2.3e-09',
'4e-05',
'1.0567e-08'
],
[
'Name1',
'1.23e-08',
'0.00034',
'-1.0567e-08'
],
[
'Name5',
'1.23e-08',
'0.00034',
'-1.0567e-08'
],
[
'Name3',
'1.3e-09',
'3e-05',
'-10.0567'
]
];

Notice that the above script's use of sort() is no different that it would
be if the key column were simple integers.

I suspect that you might be reading these numbers as strings from a text
file; when I quoted the test data in my script, Perl complained "Argument
### isn't numeric in numeric comparison", but sorted the list correctly
anyway. You can silence this warning by disabling it temporarily:

...
no warnings 'numeric';
my @sorted = sort { $b->[3] <=> $a->[3] } @numbers;
use warnings;
...

sherm--
 
J

jason_mandal

I think I am being too careless in my words. Anyways yes exponential
meant scientific notation. In my code I was using cmp instead of <=>. I
have corrected it , and it works just fine.
Thanks JUE and TORE
 
T

Tore Aursand

With something I meant nothing. The data I am trying to sort has been
listed. I tried to seek help from perldoc, and I am able to sort for
integers, but when the numbers are exponential I have no clear idea of
how to use the sort function.
Use of <=> and cmp does not help as they do not understand exponential
values. Correct me if I am wrong

Try this one:

#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;

my @data;
while ( <DATA> ) {
chomp;
my @fields = split( /\s+/, $_, 4 );
push( @data, \@fields );
}

my @sorted = sort {
$b->[3] <=> $a->[3]
} @data;

print Dumper( \@sorted );

__DATA__
Name1 123e-10 34e-5 -1.0567e-8
Name2 23e-10 4e-5 1.0567e-8
Name3 13e-10 3e-5 -1.0567e-9
Name4 12e-10 33e-5 .0011
Name5 123e-10 34e-5 -1.0567e-8
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top