Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl and GROUP by

Reply
Thread Tools

Perl and GROUP by

 
 
Radek G.
Guest
Posts: n/a
 
      02-10-2004
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-10-2004
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

 
Reply With Quote
 
 
 
 
Radek G.
Guest
Posts: n/a
 
      02-12-2004
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
>



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-12-2004
[ 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

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-12-2004
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveState Perl and MinGW [was: Perl 5.14 Windows Strawberry Perl 64 bits] Dilbert Perl Misc 0 11-10-2011 02:20 PM
how to create a group in LDAP server and add members to a particular group using JNDI? Akaketwa Java 1 09-22-2006 10:39 AM
ds0 group and channel group magnus.prem Hardware 0 07-21-2006 03:00 AM
Tablet PC User Group introduction (and Windows Mobile User Group reminder) Mauricio Freitas NZ Computing 0 07-09-2005 09:19 PM
Multiple isakmp policies (Group 1 and Group 2) rmcnutt Cisco 1 07-13-2004 09:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57