perl sorting

D

Deepu

Hi All,

I need some help in sorting a file numerically based on the numbers in
a specified column.

File contents:

[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

I need to get an output in ascending order of the numbers in column2.

Output:

[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

Thanks..
 
K

Keith Keller

I need some help in sorting a file numerically based on the numbers in
a specified column.

File contents:

[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

I need to get an output in ascending order of the numbers in column2.

Output:

[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

You should post a short but complete snippet of your Perl code that is
giving you difficulties.

--keith
 
J

Jim Gibson

Deepu said:
Hi All,

I need some help in sorting a file numerically based on the numbers in
a specified column.

File contents:

[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

I need to get an output in ascending order of the numbers in column2.

Output:

[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

Have you read the following?:

perldoc -f sort
perldoc -q sort

This is one sort function that should work:

@sorted =sort { (split(/ /,$a))[1] <=> (split(/ /,$b))[1] } @unsorted;

Making that more efficient is left as an exercise (the above performs
the split function more times than is necessary). See the perldoc
references for how.
 
G

George Mpouras

Στις 12/2/2011 12:15 πμ, ο/η Deepu έγÏαψε:
[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr


my @i;
while(<DATA>) {/:- (\S+) --/ && push @i, [$1,$_]}
foreach (sort{$a->[0] <=> $b->[0]} @i) { print $_->[1] }

__DATA__
[1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
[893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
[893.234]:- 15.75 -- tempc.tempg -> tempt.tempr
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top