Perl and GROUP by

R

Radek G.

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
 
G

Gunnar Hjalmarsson

Radek said:
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'.

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

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

Radek G.

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

regards
Radek
 
G

Gunnar Hjalmarsson

[ Do not top post ! ]
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

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!
 
J

Jürgen Exner

[Please don't top posting]
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'm sure you can.

jue
So, I guess you have problems defining a function, which for any to
arguments (from the set of
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top