sort problem

Discussion in 'Perl Misc' started by hugo leitmeier, Sep 28, 2003.

  1. Hi,

    Problem:

    Date Name Average Number
    ---- ---- ------- ------
    21.09.2003 Caesar 17.75 3748
    15.08.2003 Anton 10.34 276
    10.08.2003 Berta 15.38 138
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Caesar 10.00 374
    12.08.2003 Caesar 15.75 3748



    I like that. But, how can i accomplish that?

    Date Name Average Number
    ---- ---- ------- ------
    15.08.2003 Anton 10.34 276
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Berta 15.38 138
    10.08.2003 Caesar 10.00 374
    12.08.2003 Caesar 15.75 3748
    21.09.2003 Caesar 17.75 3748
    ---------- ------ ----- ----
    3rd (asc) 1st(asc) 2nd(asc)

    best regards

    hugo
    hugo leitmeier, Sep 28, 2003
    #1
    1. Advertising

  2. hugo leitmeier

    Greg Bacon Guest

    In article <>,
    hugo leitmeier <> wrote:

    : [...]
    : I like that. But, how can i accomplish that?
    :
    : Date Name Average Number
    : ---- ---- ------- ------
    : 15.08.2003 Anton 10.34 276
    : 27.08.2003 Anton 12.00 2765
    : 29.08.2003 Anton 11.25 2766
    : 10.08.2003 Berta 15.38 138
    : 10.08.2003 Caesar 10.00 374
    : 12.08.2003 Caesar 15.75 3748
    : 21.09.2003 Caesar 17.75 3748
    : ---------- ------ ----- ----
    : 3rd (asc) 1st(asc) 2nd(asc)

    % cat try
    #! /usr/local/bin/perl

    use warnings;
    use strict;

    my @header;
    my @rows;

    while (<DATA>) {
    if (1 .. /^----/) {
    push @header => $_;
    }
    else {
    push @rows => $_;
    }
    }

    print @header,
    map $_->[0],
    sort {
    $a->[2] cmp $b->[2] ||
    $a->[3] <=> $b->[3] ||
    $a->[1] cmp $b->[1]
    }
    map [$_, (split)[0,1,3]],
    @rows;

    __DATA__
    Date Name Average Number
    ---- ---- ------- ------
    21.09.2003 Caesar 17.75 3748
    15.08.2003 Anton 10.34 276
    10.08.2003 Berta 15.38 138
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Caesar 10.00 374
    12.08.2003 Caesar 15.75 3748
    % ./try
    Date Name Average Number
    ---- ---- ------- ------
    15.08.2003 Anton 10.34 276
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Berta 15.38 138
    10.08.2003 Caesar 10.00 374
    12.08.2003 Caesar 15.75 3748
    21.09.2003 Caesar 17.75 3748

    Hope this helps,
    Greg
    --
    Government, even in its best state, is but a necessary evil; in its worst
    state, an intolerable one. Government, like dress, is the badge of lost
    innocence; the palaces of kings are built upon the ruins of the bowers of
    paradise. -- Thomas Paine, "Common Sense"
    Greg Bacon, Sep 28, 2003
    #2
    1. Advertising

  3. Hi Greg,

    thank you very much for your help.

    best regards

    hugo



    Greg Bacon schrieb:
    > In article <>,
    > hugo leitmeier <> wrote:
    >
    > : [...]
    > : I like that. But, how can i accomplish that?
    > :
    > : Date Name Average Number
    > : ---- ---- ------- ------
    > : 15.08.2003 Anton 10.34 276
    > : 27.08.2003 Anton 12.00 2765
    > : 29.08.2003 Anton 11.25 2766
    > : 10.08.2003 Berta 15.38 138
    > : 10.08.2003 Caesar 10.00 374
    > : 12.08.2003 Caesar 15.75 3748
    > : 21.09.2003 Caesar 17.75 3748
    > : ---------- ------ ----- ----
    > : 3rd (asc) 1st(asc) 2nd(asc)
    >
    > % cat try
    > #! /usr/local/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > my @header;
    > my @rows;
    >
    > while (<DATA>) {
    > if (1 .. /^----/) {
    > push @header => $_;
    > }
    > else {
    > push @rows => $_;
    > }
    > }
    >
    > print @header,
    > map $_->[0],
    > sort {
    > $a->[2] cmp $b->[2] ||
    > $a->[3] <=> $b->[3] ||
    > $a->[1] cmp $b->[1]
    > }
    > map [$_, (split)[0,1,3]],
    > @rows;
    >
    > __DATA__
    > Date Name Average Number
    > ---- ---- ------- ------
    > 21.09.2003 Caesar 17.75 3748
    > 15.08.2003 Anton 10.34 276
    > 10.08.2003 Berta 15.38 138
    > 27.08.2003 Anton 12.00 2765
    > 29.08.2003 Anton 11.25 2766
    > 10.08.2003 Caesar 10.00 374
    > 12.08.2003 Caesar 15.75 3748
    > % ./try
    > Date Name Average Number
    > ---- ---- ------- ------
    > 15.08.2003 Anton 10.34 276
    > 27.08.2003 Anton 12.00 2765
    > 29.08.2003 Anton 11.25 2766
    > 10.08.2003 Berta 15.38 138
    > 10.08.2003 Caesar 10.00 374
    > 12.08.2003 Caesar 15.75 3748
    > 21.09.2003 Caesar 17.75 3748
    >
    > Hope this helps,
    > Greg
    hugo leitmeier, Sep 28, 2003
    #3
  4. In article <>,
    (Greg Bacon) wrote:

    > In article <>,
    > hugo leitmeier <> wrote:
    >
    > : [...]
    > : I like that. But, how can i accomplish that?
    > :
    > : Date Name Average Number
    > : ---- ---- ------- ------
    > : 15.08.2003 Anton 10.34 276
    > : 27.08.2003 Anton 12.00 2765
    > : 29.08.2003 Anton 11.25 2766
    > : 10.08.2003 Berta 15.38 138
    > : 10.08.2003 Caesar 10.00 374
    > : 12.08.2003 Caesar 15.75 3748
    > : 21.09.2003 Caesar 17.75 3748
    > : ---------- ------ ----- ----
    > : 3rd (asc) 1st(asc) 2nd(asc)
    >
    > % cat try
    > #! /usr/local/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > my @header;
    > my @rows;
    >
    > while (<DATA>) {
    > if (1 .. /^----/) {
    > push @header => $_;
    > }
    > else {
    > push @rows => $_;
    > }
    > }
    >
    > print @header,
    > map $_->[0],
    > sort {
    > $a->[2] cmp $b->[2] ||
    > $a->[3] <=> $b->[3] ||
    > $a->[1] cmp $b->[1]
    > }
    > map [$_, (split)[0,1,3]],
    > @rows;
    >
    > __DATA__
    > Date Name Average Number
    > ---- ---- ------- ------
    > 21.09.2003 Caesar 17.75 3748
    > 15.08.2003 Anton 10.34 276
    > 10.08.2003 Berta 15.38 138
    > 27.08.2003 Anton 12.00 2765
    > 29.08.2003 Anton 11.25 2766
    > 10.08.2003 Caesar 10.00 374
    > 12.08.2003 Caesar 15.75 3748
    >
    > % ./try
    > Date Name Average Number
    > ---- ---- ------- ------
    > 15.08.2003 Anton 10.34 276
    > 27.08.2003 Anton 12.00 2765
    > 29.08.2003 Anton 11.25 2766
    > 10.08.2003 Berta 15.38 138
    > 10.08.2003 Caesar 10.00 374
    > 12.08.2003 Caesar 15.75 3748
    > 21.09.2003 Caesar 17.75 3748
    >
    > Hope this helps,
    > Greg


    close, but col one is not sorting by date, which i assume the o.p.
    wanted. it just looks like it is. you can see this if you change the
    date on the last record thusly:

    __DATA__
    Date Name Average Number
    ---- ---- ------- ------
    21.09.2003 Caesar 17.75 3748
    15.08.2003 Anton 10.34 276
    10.08.2003 Berta 15.38 138
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Caesar 10.00 374
    12.10.2003 Caesar 15.75 3748

    your results:

    Date Name Average Number
    ---- ---- ------- ------
    15.08.2003 Anton 10.34 276
    27.08.2003 Anton 12.00 2765
    29.08.2003 Anton 11.25 2766
    10.08.2003 Berta 15.38 138
    10.08.2003 Caesar 10.00 374
    12.10.2003 Caesar 15.75 3748
    21.09.2003 Caesar 17.75 3748

    a slight change to your pre-sort map will fix this. additionally, i
    rearranged the order of that map to make it (i think) easier to see what
    the sort is doing:

    print @header,
    map $_->[0],
    sort {
    $a->[1] cmp $b->[1] ||
    $a->[2] <=> $b->[2] ||
    $a->[3] <=> $b->[3] ||
    $a->[4] <=> $b->[4] ||
    $a->[5] <=> $b->[5]
    }
    map [
    $_, # the whole record
    (split)[1,3], # Name, Number
    (split (/\./, (split)[0]))[2,1,0] # Date yyyy, mm, dd
    ],
    @rows;

    hth-

    --
    Michael Budash
    Michael Budash, Sep 28, 2003
    #4
  5. hugo leitmeier

    Greg Bacon Guest

    In article <>,
    Michael Budash <> wrote:

    : close, but col one is not sorting by date [...]

    Oh crud, good catch. Three cheers for YYYY-MM-DD!

    Greg
    --
    Moreover, if a free market were to be enacted in both intrastate and
    interstate markets, then the politicians and bureaucrats would find
    themselves in that most terrible (for them) condition: irrelevance.
    -- William L. Anderson
    Greg Bacon, Sep 29, 2003
    #5
    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. nobody
    Replies:
    0
    Views:
    535
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,401
    Dave Moore
    Apr 28, 2004
  3. John Black
    Replies:
    6
    Views:
    2,059
    John Harrison
    May 28, 2004
  4. Angus Comber
    Replies:
    7
    Views:
    1,154
    Richard Heathfield
    Feb 5, 2004
  5. Navin
    Replies:
    1
    Views:
    689
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page