473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Perl and GROUP by

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
Jul 19 '05 #1
4 6056
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

Jul 19 '05 #2
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" <no*****@gunnar.cc> napisal w wiadomosci
news:wc********************@newsc.telia.net...
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

Jul 19 '05 #3
[ 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

Jul 19 '05 #4
[Please don't top posting]
Radek G. wrote:
Uzytkownik "Gunnar Hjalmarsson" <no*****@gunnar.cc> napisal w
wiadomosci news:wc********************@newsc.telia.net...
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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: @ | last post by:
A benchmark in 2002 showed PHP is much slower in shell or when Apache has Mod_Perl. With the new PHP kissing Java's ass, Perl is once again the #1 CGI choice. Java is for a big team in short...
17
by: les_ander | last post by:
Hi, i am so use to perl's regular expression that i find it hard to memorize the functions in python; so i would appreciate if people can tell me some equivalents. 1) In perl: $line = "The...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # some venture into standard modules import os # print all names exported by the module print dir(os)
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
8
by: Xah Lee | last post by:
i'm starting a yahoo group for learning python. Each day, a tip of python will be shown, with the perl equivalent. For those of you perlers who always wanted to learn python, this is suitable. (i...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
2
by: shruti | last post by:
hiii all I'm tryin to call a perl script from a C program in following 2 ways- 1.By callin system function. But there's some problem because the system function is not able to executeany...
1
by: josh.kuo | last post by:
First of all, I know this is a PHP news group, so my question may not be completely appropriate here since it involves some perl. I have read this post that dealt with a similar problem that I am...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.