Ask a Question related to PERL Beginners, Design and Development.
-
Boon Chong Ang #1
sort from the smallest number to the highest number
Hi
Just say I want to sort the row by using the fifth column data as reference from the smallest the largest number, if using sort,
It will give me result like this
Abc 12.8 8 "left" 1 1.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7
but what I want is like this, what so I do to achieve this effect?
Abc 12.8 8 "left" 1 1.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7
Thank you & best regards,
ABC
Boon Chong Ang Guest
-
int Number to text Number Function / UDF ?
Is there a function that converts e.g. 3 to Three? Probably not but I just thought I'd ask. -
Number to hex ?
Looking at a few scripts, too disguise ID numbers or make them random, there appears to be a conversion to an alphanumeric number. e.g.... -
sh: bad number
Hi, I compiled a simple program with perl. But I ran into some problems. sh: campusUnderAttack.out: bad number I try to execute a program and... -
Rounder A Number Up!!
I am dividing a value by two..what i need is to round the number up if it is not a whole number. Any suggestions on how to do this ?? Help... -
tty-dev-number <-> /dev/pts
Hi, I'm writing a program under Solaris (sparc) that has to know from what machine a user is logged on who is starting that program. Reading... -
Robin Sheat #2
Re: sort from the smallest number to the highest number
On Fri, Jan 30, 2004 at 01:17:38PM +0800, Boon Chong Ang wrote:
In perldoc -f sort:> Just say I want to sort the row by using the fifth column data as
> reference from the smallest the largest number, if using sort,
sort SUBNAME LIST
sort BLOCK LIST
so you need to either write a sub that will do the comparison, or an
inline block of code. The perldoc for sort is large, and mostly composed
of examples that will make it clear.
--
Robin <robin@kallisti.net.nz> JabberID: <eythian@jabber.org>
Hostes alienigeni me abduxerunt. Qui annus est?
PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFAGeuCzTjgendttmMRAngDAJ0Zr2YEw/cdVW0N4gMMTjV+PEWVpACdFJFZ
3W8sMSQqvWJPh1vHXFPduwI=
=t4at
-----END PGP SIGNATURE-----
Robin Sheat Guest
-
Eurospace Szarindar #3
RE: sort from the smallest number to the highest number
Hi,
Here is an old answer to your problem from Randy W. Sims.
Hope it will helps.
Perhaps you would have to modify the sort to have an numerical.
Michel S
-----Message d'origine-----
De: Randy W. Sims [mailto:RandyS@ThePierianSpring.org]
Date: vendredi 23 janvier 2004 10:53
À: Bjorn Van Blanckenberg
Cc: Perl Beginners
Objet: Re: Reading tab delimited File & sort everything according item 5
of every line
On 01/23/04 03:36, Bjorn Van Blanckenberg wrote:A variation on the Swartzian transform: (read from bottom up)> the thing i'am looking for is that it is sorted by item 5 and writes
> back to the file
> with an extra line if item 5 is different
#!/usr/bin/perl
use strict;
use warnings;
print map { $_->[1] . "\n" }
sort {$a->[0] cmp $b->[0]}
map { chomp; [(split /\t/, $_)[4], $_] }
<DATA>;
__DATA__
one title state name code1 number
two title2 state2 name2 code2 number2
one title3 state3 name3 code3 number3
four title4 state4 name4 code4 number4
six title5 state5 name5 code1 number5
dip title6 state6 name6 code1 number6
fun title7 state7 name7 code2 number7
-----Message d'origine-----
De: Boon Chong Ang [mailto:BCANG@altera.com]
Date: vendredi 30 janvier 2004 06:18
À: Hanson, Rob; [email]beginners@perl.org[/email]
Objet: sort from the smallest number to the highest number
Hi
Just say I want to sort the row by using the fifth column data as reference
from the smallest the largest number, if using sort,
It will give me result like this
Abc 12.8 8 "left" 1 1.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7
but what I want is like this, what so I do to achieve this effect?
Abc 12.8 8 "left" 1 1.7
Dgf 12.3 9 "right" 4 2.6
bac 12.8 8 "left" 1 3.7
Def 13.8 9 "top" 0 19.7
gef 14.8 9 "left" 0 19.7
etg 12.8 2 "left" 7 34.7
efg 16.8 5 "right" 0 56.7
baf 32.8 7 "bottom" 5 79.8
cef 16.8 4 "right" 0 89.7
Thank you & best regards,
ABC
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Eurospace Szarindar Guest
-
Jeff 'Japhy' Pinyan #4
Re: sort from the smallest number to the highest number
On Jan 30, Boon Chong Ang said:
>Just say I want to sort the row by using the fifth column data as
>reference from the smallest the largest numberI suggest you search the web for "schwartzian transform", because that's>Abc 12.8 8 "left" 1 1.7
>Dgf 12.3 9 "right" 4 2.6
>bac 12.8 8 "left" 1 3.7
>Def 13.8 9 "top" 0 19.7
>gef 14.8 9 "left" 0 19.7
>etg 12.8 2 "left" 7 34.7
>efg 16.8 5 "right" 0 56.7
>baf 32.8 7 "bottom" 5 79.8
>cef 16.8 4 "right" 0 89.7
what I'm about to show you:
my @sorted =
map $_->[0], # get back the original string
sort { $a->[6] <=> $b->[6] } # sort on 5th field (0 is the original)
map [ $_, split ], # array ref [string, fields]
@data;
Read it from the bottom up.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest



Reply With Quote

