CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Advanced sorting question.

 



perl-novice
New User

Jul 28, 2009, 12:14 PM

Post #1 of 5 (842 views)
Advanced sorting question. Can't Post

I'm trying to program in Perl a way to sort a table of data by one specific column, similar to how you sort in Excel ("Data"-"Sort"-"Sort by"). Say you have a table as follows:
Var1 Var2 Var3 Total
Bill 11 13 18 42
Bob 20 16 27 63
John 8 23 22 53
Mark 32 24 17 73
I want to sort a table by the "Total" column, so the end result looks like this:

Var1 Var2 Var3 Total
Bill 11 13 18 42
John 8 23 22 53
Bob 20 16 27 63
Mark 32 24 17 73

I am aware of sorting arrays and such in Perl, but my thinking is that if I sort the Total column, it will only sort that column, and not adjust the rest of the table accordingly. Example:

Var1 Var2 Var3 Total
Bill 11 13 18 42
Bob 20 16 27 53
John 8 23 22 63
Mark 32 24 17 73

How do I code this correctly?

Thanks!


KevinR
Veteran


Jul 28, 2009, 1:07 PM

Post #2 of 5 (840 views)
Re: [perl-novice] Advanced sorting question. [In reply to] Can't Post

It is possible to sort the data as you require.
-------------------------------------------------


(This post was edited by KevinR on Jul 28, 2009, 1:10 PM)


KevinR
Veteran


Jul 28, 2009, 1:28 PM

Post #3 of 5 (835 views)
Re: [perl-novice] Advanced sorting question. [In reply to] Can't Post

Here is a quick example of one way it can be done:


Code
my @sorted = map {$_->[0]} sort{ $a->[1] <=> $b->[1] } map{chomp; [$_,(split(/\s+/))[4]]} <DATA>; 
print "$_\n" for @sorted;
__DATA__
Bill 11 13 18 42
Bob 20 16 27 63
John 8 23 22 53
Mark 32 24 17 73


That is a good way if your file isn't too big because it has to read the entire file into memory first. Actually all sorting will have to read the file into memory but some ways might be more memory efficient and some ways might be more time efficient. The code I posted is a good balance between memory and efficiency as long as the file isn't too big to fit into memory. Some reading if you are interested:

http://www.perlmonks.org/?node=128722
-------------------------------------------------


1arryb
User

Jul 29, 2009, 8:48 AM

Post #4 of 5 (822 views)
Re: [KevinR] Advanced sorting question. [In reply to] Can't Post

Kevin,

Clever code like that is why people hate perl Wink. Here's a deconstructed version (slightly simplified) that mere mortals might understand:

Code
#!/usr/bin/perl 

use strict;
use warnings;

# Read the data into a 2D array.
my @data = map { chomp; [split(/\s+/)] } <DATA>;

# Sort it on the 'total'.
my @sorted = sort{ $a->[4] <=> $b->[4] } @data;

# Print it.
map { print join(' ', @$_), "\n" } (@sorted);

__DATA__
Bill 11 13 18 42
Bob 20 16 27 63
John 8 23 22 53
Mark 32 24 17 73


Cheers,

Larry


KevinR
Veteran


Jul 29, 2009, 10:04 AM

Post #5 of 5 (818 views)
Re: [1arryb] Advanced sorting question. [In reply to] Can't Post

I understand what you're getting at a Larry, but I did provide a link that shows and explains most if not all of the ways perl can be used to sort data. Hopefully that mitigates by succint example. Smile

I do appreciate you taking the time to post a more readable example based on my example. Thank you.
-------------------------------------------------


(This post was edited by KevinR on Jul 29, 2009, 10:05 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives