Sorting a simple database

Discussion in 'Perl' started by Duke of Hazard, Sep 12, 2003.

  1. 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
    Duke of Hazard, Sep 12, 2003
    #1
    1. Advertising

  2. Duke of Hazard

    Purl Gurl Guest

    Purl Gurl wrote:

    > 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
    Purl Gurl, Sep 12, 2003
    #2
    1. Advertising

  3. Duke of Hazard

    John Bokma Guest

    Purl Gurl wrote:

    > 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)

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
    John Bokma, Sep 12, 2003
    #3
  4. Duke of Hazard

    Purl Gurl Guest

    John Bokma wrote:

    > Purl Gurl wrote:
    > > 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
    Purl Gurl, Sep 12, 2003
    #4
  5. Duke of Hazard

    John Bokma Guest

    Purl Gurl wrote:

    > 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 ;-)

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
    John Bokma, Sep 12, 2003
    #5
  6. Duke of Hazard

    Purl Gurl Guest

    John Bokma wrote:

    > Purl Gurl wrote:


    (snipped)

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


    > 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
    Purl Gurl, Sep 12, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    594
  2. Replies:
    2
    Views:
    1,427
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    384
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    474
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    157
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page