Perl and GROUP by

Discussion in 'Perl' started by Radek G., Feb 10, 2004.

  1. Radek G.

    Radek G. Guest

    Hello
    I must implemend sorting in perl (that's not too dificult;) ) but...i must
    sort some array with grouping (like in sql).
    I meen... For Example I have array with first and last names:
    @names= ('a a','a b','a c',' b b', 'b d' ). And i must sort it first by
    first name from 'z' to 'a' and by last name from 'a' to 'z'.
    so after sorting i recive something like:
    @names=

    b b
    b d
    a a
    a b
    a c

    Anyone have some beautyfull "one line" code to do it (sort , map, grep).

    Regards Radek
     
    Radek G., Feb 10, 2004
    #1
    1. Advertising

  2. Radek G. wrote:
    > I must implemend sorting in perl (that's not too dificult;) )
    > but...i must sort some array with grouping (like in sql).
    > I meen... For Example I have array with first and last names:
    > @names= ('a a','a b','a c',' b b', 'b d' ). And i must sort it
    > first by first name from 'z' to 'a' and by last name from 'a' to
    > 'z'.


    <snip>

    > Anyone have some beautyfull "one line" code to do it (sort , map,
    > grep).


    Would you mind checking out the docs for the sort() function?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 10, 2004
    #2
    1. Advertising

  3. Radek G.

    Radek G. Guest

    I've check...smart man.
    So if you are so smart - can you do it with one sort?
    I don't.

    regards
    Radek

    Uzytkownik "Gunnar Hjalmarsson" <> napisal w wiadomosci
    news:wc5Wb.82194$...
    > Radek G. wrote:
    > > I must implemend sorting in perl (that's not too dificult;) )
    > > but...i must sort some array with grouping (like in sql).
    > > I meen... For Example I have array with first and last names:
    > > @names= ('a a','a b','a c',' b b', 'b d' ). And i must sort it
    > > first by first name from 'z' to 'a' and by last name from 'a' to
    > > 'z'.

    >
    > <snip>
    >
    > > Anyone have some beautyfull "one line" code to do it (sort , map,
    > > grep).

    >
    > Would you mind checking out the docs for the sort() function?
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
     
    Radek G., Feb 12, 2004
    #3
  4. [ Do not top post ! ]

    Radek G. wrote:
    > I've check...smart man.
    > So if you are so smart - can you do it with one sort?


    Talking about "smart": http://www.catb.org/~esr/faqs/smart-questions.html

    > I don't.


    In that case, show us what you've tried so far, and let us know why
    you have difficulties in applying the docs examples to your problem.
    Post the code you have, and somebody may be willing to help you make
    the necessary corrections.

    But do *not* just ask people to do your job!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 12, 2004
    #4
  5. [Please don't top posting]
    Radek G. wrote:
    > Uzytkownik "Gunnar Hjalmarsson" <> napisal w
    > wiadomosci news:wc5Wb.82194$...
    >> Radek G. wrote:
    >>> I must implemend sorting in perl (that's not too dificult;) )
    >>> but...i must sort some array with grouping (like in sql).
    >>> I meen... For Example I have array with first and last names:
    >>> @names= ('a a','a b','a c',' b b', 'b d' ). And i must sort it
    >>> first by first name from 'z' to 'a' and by last name from 'a' to
    >>> 'z'.

    >>
    >> Would you mind checking out the docs for the sort() function?


    > I've check...smart man.
    > So if you are so smart - can you do it with one sort?


    That was not apparent from your first posting. Typically people coming this
    way do not check the docs first.
    Furthermore, if you would have checked the docs, then I would have expected
    your question to be different. I would have expected you to ask about the
    comparison function for you unusual requirements.

    Furthermore "perldoc -q sort" has even an example that almost fits your
    bill:
    If you need to sort on several fields, the following paradigm is
    useful.
    @sorted = sort { field1($a) <=> field1($b) ||
    field2($a) cmp field2($b) ||
    field3($a) cmp field3($b)
    } @data;

    Now, all you have to do is define field1() and field2() and watch out for
    the right arrangement of arguments in order to get the lesser-values-first
    versus higher-value-first sequence.
    This may not be the most efficient solution but it'll do the job. To
    increase efficiency just split $a nd $b once and then use the individual
    parts in the actual comparison.

    > I don't.


    I'm sure you can.

    jue
    So, I guess you have problems defining a function, which for any to
    arguments (from the set of
     
    Jürgen Exner, Feb 12, 2004
    #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. Akaketwa
    Replies:
    1
    Views:
    5,049
    impaler
    Sep 22, 2006
  2. Replies:
    0
    Views:
    571
  3. christopher taylor
    Replies:
    0
    Views:
    437
    christopher taylor
    Sep 17, 2008
  4. cpld-fpga-asic
    Replies:
    13
    Views:
    1,220
    rickman
    Jul 6, 2009
  5. Dag Sunde
    Replies:
    0
    Views:
    136
    Dag Sunde
    Nov 18, 2005
Loading...

Share This Page