Connecting Tech Pros Worldwide Forums | Help | Site Map

Perl and GROUP by

Radek G.
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Gunnar Hjalmarsson
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Perl and GROUP by


Radek G. wrote:[color=blue]
> 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'.[/color]

<snip>
[color=blue]
> Anyone have some beautyfull "one line" code to do it (sort , map,
> grep).[/color]

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Radek G.
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Perl and GROUP by


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" <noreply@gunnar.cc> napisal w wiadomosci
news:wc5Wb.82194$dP1.216693@newsc.telia.net...[color=blue]
> Radek G. wrote:[color=green]
> > 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'.[/color]
>
> <snip>
>[color=green]
> > Anyone have some beautyfull "one line" code to do it (sort , map,
> > grep).[/color]
>
> Would you mind checking out the docs for the sort() function?
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>[/color]


Gunnar Hjalmarsson
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Perl and GROUP by


[ Do not top post ! ]

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

Talking about "smart": http://www.catb.org/~esr/faqs/smart-questions.html
[color=blue]
> I don't.[/color]

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

Jürgen Exner
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Perl and GROUP by


[Please don't top posting]
Radek G. wrote:[color=blue]
> Uzytkownik "Gunnar Hjalmarsson" <noreply@gunnar.cc> napisal w
> wiadomosci news:wc5Wb.82194$dP1.216693@newsc.telia.net...[color=green]
>> Radek G. wrote:[color=darkred]
>>> 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'.[/color]
>>
>> Would you mind checking out the docs for the sort() function?[/color][/color]
[color=blue]
> I've check...smart man.
> So if you are so smart - can you do it with one sort?[/color]

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.
[color=blue]
> I don't.[/color]

I'm sure you can.

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


Closed Thread