Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Non-DB table sorting

by Xaositect (Friar)
on Aug 10, 2002 at 03:04 UTC ( #189128=perlquestion: print w/replies, xml ) Need Help??

Xaositect has asked for the wisdom of the Perl Monks concerning the following question:

I'm putting together a web page table, and using it (among other things) as a chance to learn some perl. I'd like to provide the user with sort-by-column functionality.
The data set isn't big enough to warrant a real database behind the scenes, but it's also big enough to make client-side JavaScript sorting questionable. (Tendancy to "hang" while it sorts, at least if a page is reloading the user knows something is happening)

The obvious language-independant server-side approach here is to load the entire dataset into a 2D array, sort, and iterate through the first dimension while printing table rows.
This seemed like a straightforward task, perl provides a sort function that accepts a sub to compare (sort) with. Loading the data into a list of lists is easy enough, however, I'm a little confused regarding how the reference syntax would work in this sort sub.

This seems like a common type of task, sorting by something other than the first index in a list of lists, does anyone have a solution they're particularly proud of?

Replies are listed 'Best First'.
(tye)Re: Non-DB table sorting
by tye (Sage) on Aug 10, 2002 at 03:33 UTC

    I like sorting an array of indices for stuff like that:

    my @data= ([...],[...],...); my $idx= 3; my @idx= sort { $data[$a][$idx] <=> $data[$b][$idx] }, 0..$#data; for my $row ( @data[@idx] ) { for my $value ( @{$_} ) { ... } } # or print $q->table( map { $q->Tr( map { $q->td($_) } @{$_} ) } @data[@idx] );
    That way you can use an array slice to get each column in the "sorted" order. Though, with each record held in single anonymous array, it has little advantage (unless you wish to keep track of the "original" order for some reason).

    As for figuring out complex data structure dereferencing syntax, I found that References Quick Reference made it so I never had any problems with that again.

            - tye (but my friends call me "Tye")
Re: Non-DB table sorting
by grep (Monsignor) on Aug 10, 2002 at 06:21 UTC
    If you want the convenience of SQL sorting without a whole DB engine - you can also look at DBD::CSV which does support SQL ORDER BY

    This is also quite nice if you have experience with SQL



    grep
    Just me, the boy and these two monks, no questions asked.
Re: Non-DB table sorting
by perrin (Chancellor) on Aug 10, 2002 at 03:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://189128]
Approved by mattriff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2020-10-23 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (234 votes). Check out past polls.

    Notices?