Sort and remove duplicates

Discussion in 'Perl Misc' started by JimJx, Sep 28, 2007.

  1. JimJx

    JimJx Guest

    Hi everyone,

    Got what should be an easy problem.

    I have a db (MySQL) that I need to sort on the first field and remove
    dups from.

    For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
    Green,1,2,3,4,5 and Green,2,2,2,2,2

    How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??

    I know that there is a simple way to do this but my brain is not
    functioning right now.

    Thanks!
    Jim
     
    JimJx, Sep 28, 2007
    #1
    1. Advertising

  2. JimJx

    Greg Bacon Guest

    In article <>,
    JimJx <> wrote:

    : I have a db (MySQL) that I need to sort on the first field and
    : remove dups from.
    :
    : For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5
    : and Green,1,2,3,4,5 and Green,2,2,2,2,2
    :
    : How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;
    : Red,1,2,3,4,5??

    Why does the Green row get 1,2,3,4,5 on the output?

    Greg
    --
    The list of stable paper currencies built by central bankers is as
    short as the list of stable democracies built by armed invaders.
    -- Bill Bonner
     
    Greg Bacon, Sep 28, 2007
    #2
    1. Advertising

  3. JimJx

    JimJx Guest

    On Sep 28, 10:33 am, (Greg Bacon) wrote:
    > In article <>,
    > JimJx <> wrote:
    >
    > : I have a db (MySQL) that I need to sort on the first field and
    > : remove dups from.
    > :
    > : For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5
    > : and Green,1,2,3,4,5 and Green,2,2,2,2,2
    > :
    > : How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;
    > : Red,1,2,3,4,5??
    >
    > Why does the Green row get 1,2,3,4,5 on the output?
    >
    > Greg
    > --
    > The list of stable paper currencies built by central bankers is as
    > short as the list of stable democracies built by armed invaders.
    > -- Bill Bonner


    Because Green would be a dup and 1,2,3,4,5 comes before 2,2,3,4,5
     
    JimJx, Sep 28, 2007
    #3
  4. JimJx

    Ben Morrow Guest

    Quoth JimJx <>:
    > Hi everyone,
    >
    > Got what should be an easy problem.
    >
    > I have a db (MySQL) that I need to sort on the first field and remove
    > dups from.
    >
    > For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
    > Green,1,2,3,4,5 and Green,2,2,2,2,2
    >
    > How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
    >
    > I know that there is a simple way to do this but my brain is not
    > functioning right now.


    perldoc DBI
    perldoc -q duplicate
    (possibly perldoc -f sort as well)

    When you've had a go, post your code and we'll help you fix it.

    Ben
     
    Ben Morrow, Sep 28, 2007
    #4
  5. JimJx

    Greg Bacon Guest

    In article <>,
    JimJx <> wrote:

    : On Sep 28, 10:33 am, (Greg Bacon) wrote:
    :
    : > Why does the Green row get 1,2,3,4,5 on the output?
    :
    : Because Green would be a dup and 1,2,3,4,5 comes before 2,2,3,4,5

    Ah. I thought you wanted to remove duplicates in the other columns
    too. (The five 2s threw me off.)

    Glenn Jackman's answer is what you want.

    Greg
    --
    To admit that labor needs protection is to acknowledge its inferiority.
    -- Henry George, Protection or Free Trade
     
    Greg Bacon, Sep 28, 2007
    #5
  6. On Fri, 28 Sep 2007 06:10:54 -0700, JimJx wrote:

    > Hi everyone,
    >
    > Got what should be an easy problem.
    >
    > I have a db (MySQL) that I need to sort on the first field and remove
    > dups from.
    >
    > For example, let's say I have Red,1,2,3,4,5 and Blue,1,2,3,4,5 and
    > Green,1,2,3,4,5 and Green,2,2,2,2,2
    >
    > How can I come out with Blue,1,2,3,4,5;Green,1,2,3,4,5;Red,1,2,3,4,5??
    >
    > I know that there is a simple way to do this but my brain is not
    > functioning right now.


    select field1, min(field2), min(field3) ... from table group by field1;

    HTH,
    M4
     
    Martijn Lievaart, Sep 28, 2007
    #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. Steven Bethard
    Replies:
    11
    Views:
    1,062
    Alex Martelli
    Feb 7, 2005
  2. Jesper Mortensen
    Replies:
    1
    Views:
    502
  3. Navin
    Replies:
    1
    Views:
    748
    Ken Schaefer
    Sep 9, 2003
  4. Laphan
    Replies:
    2
    Views:
    178
    Laphan
    Feb 12, 2008
  5. Susan
    Replies:
    6
    Views:
    154
    Xicheng
    Jan 27, 2006
Loading...

Share This Page