Sorting a simple database

D

Duke of Hazard

I have a simple database like this:

Agassi`6.0
Sampras`5.5
Tennis_novice`3.0
Tennis_beginner`2.0

I would like to sort it numerically by the second column so it looks
like this:

Tennis_beginner`2.0
Tennis_novice`3.0
Sampras`5.5
Agassi`6.0

I realize the "sort" command works for arrays, but how could I get it
to work for this type of associative array (assuming that's what I
need to do)?

Thanks,

Faraz
 
P

Purl Gurl

Purl said:
Duke wrote:
(snipped)

Simple use of substring fits your parameters nicely.


Here are some benchmark tests which may be of interest.


Purl Gurl
--

#!perl

print "Content-type: text/plain\n\n";

use Benchmark;

print "Run One:\n\n";
&Time;

print "\n\nRun Two:\n\n";
&Time;

print "\n\nRun Three:\n\n";
&Time;

sub Time
{
timethese (10000,
{
'Randal' =>
'@Array = qw (Agassi`6.0 Sampras`5.5 Tennis_novice`3.0 Tennis_beginner`2.0);
@Array = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, /(\d+\.\d+)/ ] }
@Array;',

'Purl_Gurl' =>
'@Array = qw (Agassi`6.0 Sampras`5.5 Tennis_novice`3.0 Tennis_beginner`2.0);
@Array = sort
{
substr ($a, rindex ($a, "`") + 1)
<=>
substr ($b, rindex ($b, "`") + 1)
}
@Array;',
} );
}


PRINTED RESULTS:
________________

Run One:

Benchmark: timing 10000 iterations of Purl_Gurl, Randal...
Purl_Gurl: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12195.12/s
Randal: 2 wallclock secs ( 1.20 usr + 0.00 sys = 1.20 CPU) @ 8333.33/s


Run Two:

Benchmark: timing 10000 iterations of Purl_Gurl, Randal...
Purl_Gurl: 0 wallclock secs ( 0.83 usr + 0.00 sys = 0.83 CPU) @ 12048.19/s
Randal: 1 wallclock secs ( 1.21 usr + 0.00 sys = 1.21 CPU) @ 8264.46/s


Run Three:

Benchmark: timing 10000 iterations of Purl_Gurl, Randal...
Purl_Gurl: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12195.12/s
Randal: 1 wallclock secs ( 1.21 usr + 0.00 sys = 1.21 CPU) @ 8264.46/s
 
J

John Bokma

Purl said:
John Bokma wrote:

[snip buggy code by me]
@Array = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, /(\d+\.\d+)^/ ] }

^ should be $ (thanks)
{ [ $_, /(\d+\.\d+)/ ] }

Nah, better safe than sorry, I would use the $, it is self documenting
code and perhaps (?) faster.

Thanks Gurl ;-). (Not sleeping doesn't help writing clear code)
 
P

Purl Gurl

John said:
Purl said:
John Bokma wrote:
(snipped)

map { [ $_, /(\d+\.\d+)^/ ] }
^ should be $ (thanks)
{ [ $_, /(\d+\.\d+)/ ] }
Nah, better safe than sorry, I would use the $, it is self documenting
code and perhaps (?) faster.

Both of our code examples are slightly flawed if the originating
author or a reader, do not know to chomp off a \n if an array
is built from a read file.

However, for these circumstances of a numerical sort, a trailing
newline has no effect and does nicely show in a print.


Purl Gurl
--
#!perl

while (<DATA>)
{ push (@Array, $_); }

@Array = sort
{
substr ($a, rindex ($a, "`") + 1)
<=>
substr ($b, rindex ($b, "`") + 1)
}
@Array;

print @Array;

__DATA__
Agassi`6.0
Sampras`5.5
Tennis_novice`3.0
Tennis_beginner`2.0


PRINTED RESULTS:
________________

Tennis_beginner`2.0
Tennis_novice`3.0
Sampras`5.5
Agassi`6.0
 
J

John Bokma

Purl said:
Both of our code examples are slightly flawed if the originating
author or a reader, do not know to chomp off a \n if an array
is built from a read file.

true, true
However, for these circumstances of a numerical sort, a trailing
newline has no effect and does nicely show in a print.

[snip]

You really love your Purl, Gurl ;-)
 
P

Purl Gurl

John said:
Purl Gurl wrote:
(snipped)
You really love your Purl, Gurl ;-)

You have to love Perl or your choice of programming language,
to cope with some of the twits populating groups like this.

I like Perl, old ANSII C, DOS batch and a little Visual Basic
via Macro Scripting (not true VB).

My enjoyment is in figuring out solutions for programming problems.
This is intellectually challenging and challenges your logic. You
can be sure I make a lot of mistakes, from which I learn, and am
rewarded upon success.

My true joy is inventing a better already invented wheel.

Yes, reinventing the wheel, is very rewarding. If this was not so,
our cars would have wheels chiseled out of stone. Still, there are
some who appear to still live in the Stone Age, hence Randal's
Betty, Wilma, Barney and endearing, Fred.

Heh!

Purl Gurl
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top