473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6067
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******** ************@ne wsc.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******** ************@ne wsc.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
4471
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 time to develope something slow. ASP is a joke. PHP is a kid.
17
1986
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 food is under the bar in the barn."; if ( $line =~ /foo(.*)bar/ ) { print "got <$1>\n"; } in python, I don't know how I can do this?
4
1424
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
4758
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 great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
41
3520
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 pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
8
1295
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 started it because i always wanted to switch to python but too lazy and always falling back to a lang i am an expert at, but frustrated constantly by its inanities and incompetences.) to subscribe, go to:...
0
9737
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. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
2
3489
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 command...it just returns -1. 2.By using the code given in perldoc perlembed(perl documentation for embedding perl in C)
1
3729
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 having now: http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thre... Summary of my problem: I need to have a perl script that encrypts a string, write the output to a file,
21
34375
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8251
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5839
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.