Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Descending sort

Reply
Thread Tools

Descending sort

 
 
Colossus
Guest
Posts: n/a
 
      12-03-2003
Hi,

I have a flat database file this way:

A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39
A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
A14829.PE1 AF042219.PE1 HBG004257 2.03983 110

The fields are tab separated. I want it this way:
A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
A14829.PE1 AF042219.PE1 HBG004257 2.03983 110
A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39

I grabbed this piece of code but it doesn't work:

foreach my $MyData (sort { $a->[1] <=> $b->[1] } map{[$_, /^(\d+)/]} @temp)
{
printf "%-15s%5d: %-s\n", ' ', $MyData->[1], $MyData->[0];
}

I understand the problem is regular expression in map. I also
tried to use $b->4 <=> $a->4 because the field I have to order by
is at fourth position but nothing to do ! Could anyone help me please ?

--
Bye,
Colossus

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      12-03-2003
Colossus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi,
>
> I have a flat database file this way:
>
> A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
> A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
> A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39
> A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
> A14829.PE1 AF042219.PE1 HBG004257 2.03983 110
>
> The fields are tab separated. I want it this way:
> A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
> A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
> A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
> A14829.PE1 AF042219.PE1 HBG004257 2.03983 110
> A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39


What is this? A puzzle to amuse the regulars? Believe me, we can do
without those.

No column in your proposed output data is sorted, neither ascending
nor descending. An example fine, but it is is never enough to explain
your intentions. You must also explain in words what you want to do.

> I grabbed this piece of code but it doesn't work:
>
> foreach my $MyData (sort { $a->[1] <=> $b->[1] } map{[$_, /^(\d+)/]} @temp)
> {
> printf "%-15s%5d: %-s\n", ' ', $MyData->[1], $MyData->[0];


Your sprintf format provides for three data elements, but you're only
printing two. It can never print anything like your proposed output.

> }


That's a Schwartz transform for sorting strings that contain a number
into descending order. What on earth made you think it is applicable
to your problem?

> I understand the problem is regular expression in map. I also


You'd need a pretty formidable regex to extract fields that sort
according to your example. Why do you believe you need a regex
to extract sort fields? Sort field extraction is trivially done
through split(), it is not your problem.

> tried to use $b->4 <=> $a->4 because the field I have to order by


If you did try that, Perl would have told you it is a syntax error.
Please take some care in reporting what you tried. You may have tried
"$b->[4] <=> $a->[4]", but that is still nonsense because each array
has only two elements.

> is at fourth position but nothing to do ! Could anyone help me please ?


Your example output is consistent with a combined sort strategy: Sort
(alphabetically, descending) by the first field. If the first fields
are equal, sort (numerically, ascending) by the fifth field.

Here is how it can be done:

#!/usr/bin/perl
use strict; use warnings; $| = 1; # @^~`

# build an array of arrays, each containing the fields of one line
my @raw = map [ split ], <DATA>;

# sort them
my @sorted = sort { $a->[ 0] cmp $b->[ 0] or $b->[ 4] <=> $a->[ 4] } @raw;

# show the output
use Text::Table;
my $tb = Text::Table->new( ( '') x 5);
$tb->load( @sorted);
print $tb;

__DATA__
A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39
A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
A14829.PE1 AF042219.PE1 HBG004257 2.03983 110

Anno
 
Reply With Quote
 
 
 
 
Colossus
Guest
Posts: n/a
 
      12-03-2003
Anno Siegel wrote:
> Your example output is consistent with a combined sort strategy: Sort
> (alphabetically, descending) by the first field. If the first fields
> are equal, sort (numerically, ascending) by the fifth field.


I took that code from the newsgroup perl.beginners in a message of 2002:

http://groups.google.com/groups?hl=e...com%26rnum%3D6

I tried to adapt to my problem but without success. Could you please explain
me why I have to sort alphabetically the first fields and then sort by the
fifth one ? Can I sort directly by the fifth one ?

Thank you so much for your answer.
--
Bye,
Colossus

 
Reply With Quote
 
Colossus
Guest
Posts: n/a
 
      12-03-2003
@array = ("A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39",
"A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110",
"A02759.PE1 XLA298150.APP HBG000051 0.12417 592",
"A08691.RAP1A AF032713.PE1 HBG009351 0.63293 333",
"A08802.PE1 AF349034.PE1 HBG000071 0.82133 386",
"A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39",
"A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386",
"A14829.PE1 AF042219.PE1 HBG004257 2.03983 110");

my @sorted = sort { $a->[ 0] cmp $b->[ 0] or $b->[ 4] <=> $a->[ 4] }
@array;

foreach $indice(0..$#sorted)
{
print $sorted[$indice],"\n";
}

Output:

A00469.PE1 SSGGH.PE1 HBG011318 1.26943 39
A00469.PE1 SGGH2.PE1 HBG011318 1.26943 110
A02759.PE1 XLA298150.APP HBG000051 0.12417 592
A08691.RAP1A AF032713.PE1 HBG009351 0.63293 333
A08802.PE1 AF349034.PE1 HBG000071 0.82133 386
A14829.PE1 SSAPOLAI.PE1 HBG004257 1.88791 39
A14829.PE1 DRAPLIPAI.PE1 HBG004257 1.96625 386
A14829.PE1 AF042219.PE1 HBG004257 2.03983 110

That is exactly the same starting array.
--
Bye,
Colossus

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-03-2003
Colossus wrote:
> That is exactly the same starting array.


Have you possibly considered to "grab" some Perl documentation, rather
than grabbing various pieces of code that you don't understand?

A suitable starter might be

perldoc -f sort

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-03-2003
Colossus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Your example output is consistent with a combined sort strategy: Sort
> > (alphabetically, descending) by the first field. If the first fields
> > are equal, sort (numerically, ascending) by the fifth field.

>
> I took that code from the newsgroup perl.beginners in a message of 2002:
>
> http://groups.google.com/groups?hl=e...com%26rnum%3D6
>
> I tried to adapt to my problem but without success. Could you please explain
> me why I have to sort alphabetically the first fields and then sort by the
> fifth one ?


What's to explain? *You* gave the example, and that's a way to reproduce
your example.

> Can I sort directly by the fifth one ?


You can, but that won't give you the sequence of your example.

Anno
 
Reply With Quote
 
Colossus
Guest
Posts: n/a
 
      12-03-2003
Gunnar Hjalmarsson wrote:

> A suitable starter might be


What you call suitable is a very difficult
explanation on how to sort an array of more fields.
--
Bye,
Colossus

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-03-2003
Colossus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Gunnar Hjalmarsson wrote:
>
> > A suitable starter might be

>
> What you call suitable is a very difficult
> explanation on how to sort an array of more fields.


So you want to use the sort function, but the documentation is too
difficult? "Suitable" isn't the same as "simple".

Anno
 
Reply With Quote
 
Colossus
Guest
Posts: n/a
 
      12-03-2003
Anno Siegel wrote:

>So you want to use the sort function, but the documentation is too
>difficult? "Suitable" isn't the same as "simple".


Still your code doesn't work, I think it is much
better for you not to answer at all. Did you read
the documentation about sort ? You write perl code
that works only in your brain.
--
Bye,
Colossus

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-03-2003
Colossus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> >So you want to use the sort function, but the documentation is too
> >difficult? "Suitable" isn't the same as "simple".

>
> Still your code doesn't work, I think it is much
> better for you not to answer at all. Did you read
> the documentation about sort ? You write perl code
> that works only in your brain.


Sure it works. You botched it when you gave my sort routine an array
of strings instead an array of arrays of fields. If you had switched
on warnings or strictures, you'd already know that something was
wrong. You should always switch them on in troublesome code.

I get the impression you have very little experience in programming.
There's nothing wrong with that, but you have probably bitten off
a little more than you can chew at the moment. Get acquainted with
the basics first. What *is* wrong is to over-estimate your own
abilities.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort in the Descending Order Luigi Napolitano C Programming 7 12-22-2004 03:32 PM
Sort in descending order with index Luigi Napolitano C++ 2 12-17-2004 10:49 PM
Sort in the Descending Orger with Index Luigi Napolitano Java 1 12-17-2004 12:27 PM
descending sort on files in 'save as' dialogue NewB Computer Support 6 11-05-2004 07:43 AM
Ascending & Descending sort in datagrid Steve Chatham ASP .Net 2 02-19-2004 09:31 PM



Advertisments