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:
Sorting by a different Variable

 



andy7t
User

Aug 23, 2003, 1:55 AM

Post #1 of 10 (1552 views)
Sorting by a different Variable Can't Post

Hello,

On many websites, there are options (in a table) to sort by different pieces of data:

e.g

Name (sort) Age (sort) DOB (sort)

And by clicking sort they are sorted according to which one the user clicks.

But my problem is how do you do this when your opening just one file- for example Name, then opening all the other from it.

I think an example is needed:


Code
   

print "<TABLE> <TR><TD>

Name (<a href=?sortname>sort</a>)

<TD> Age (<a href=?sortage>sort</a>)\n";
open NAMES "data/listofnames.txt";
@names = <NAMES>;
if($ENV{'QUERY_STRING'} eq "sortname")
{@names = sort(@names); }
close NAMES;

foreach $names (@names)
{
open NAMEDATA, "data/$names.txt";
@namedata = <NAMEDATA>;
close NAMEDATA;


print "<TR> $names <TD> $namedata[2]\n";

#NAMEDATA[2] IS THE PERSONS AGE


So i can sort by names but not by ages.


KevinR
Veteran


Aug 23, 2003, 1:53 PM

Post #2 of 10 (1547 views)
Re: [andy7t] Sorting by a different Variable [In reply to] Can't Post

it looks you might be going about this in a convoluted way. If you can re-structure your data files this should be very easy. What you need are presorted files already on the server that you open depending on what the sort method is. The only time you need to sort is when a new entry is added. You put the new entry in all of the presorted files, then re-sort the files, then re-print them to disk already sorted by their respective sort criteria (age, name, whatever, whatever).

You presorted name file might look like this:

Andy,23,180
Bob,19,123
Fred,44,230
Joe,33,145

Your presorted age file might look like this:

Bob,19,123
Andy,23,180
Joe,33,145
Fred,44,230

Now this situation is not always possible, but if it is it will make your life much easier and your scripts run faster since the sorting may only need to be done once in a while (when a new entry is added) instead of everytime the files are opened.
-------------------------------------------------


andy7t
User

Aug 24, 2003, 10:36 AM

Post #3 of 10 (1541 views)
Re: [KevinR] Sorting by a different Variable [In reply to] Can't Post

Thanks.

This would not be sutible for this event as the output is the results from a search- therefore the results are unpredictable.

But, you did give me an idea:

Whenever the script is run i could make the output to a file, and then do as you suggested.

Thank


davorg
Thaumaturge / Moderator

Aug 26, 2003, 1:11 AM

Post #4 of 10 (1533 views)
Re: [andy7t] Sorting by a different Variable [In reply to] Can't Post

Maybe you should think about storing your data in a database.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Jane
Novice

Aug 26, 2003, 4:56 AM

Post #5 of 10 (1528 views)
Re: [andy7t] Sorting by a different Variable [In reply to] Can't Post

Hello andy7t. It's quite simple to do what you're asking by customising Perl's sort function.

The following is based on code from the perlfunc docs which you should study.
'cmp' is for comparing strings. '<=>' is for comparing numbers.
The logical OR operator let's us choose the appropriate comparison method.
@newlines is a slice of @lines sorted according to what's stored in @particulars.
If you want the lines upside down, swap the positions of $a and $b in the sort function.
And remember, when you name functions after user input it can be dangerous, so untaint your input.



Code
   

print qq(<a href="$progurl?sort=0">Name</a>
<a href="$progurl?sort=1">Age</a>
<a href="$progurl?sort=2">DOB</a><br>);

my( @lines, @particulars, @newlines );
while( <DATA> ){
push @lines, $_;
my @members = split;
push @particulars, $members[$q->param('sort')];
}

@newlines = @lines[sort { $particulars[$a] <=> $particulars[$b] || $particulars[$a] cmp $particulars[$b] } 0..$#lines ];

print "$_<br>" for @newlines;

__DATA__
Bob 19 123
Andy 23 180
Joe 33 145
Fred 44 230

Jane.
Think |negatively :(|
map{$,--%(5>>1)^0?push@y,$_:unshift@y,$_}sort(Hacker=>another=>Perl=>Just);for(1&1=>1|0){push@y,shift@y}print"@y";


Paul
Enthusiast

Aug 26, 2003, 4:35 PM

Post #6 of 10 (1519 views)
Re: [Jane] Sorting by a different Variable [In reply to] Can't Post

You should verify and validate the contents of $q->param('sort'). Never assume what is contianed in user input.


Jane
Novice

Aug 27, 2003, 1:41 AM

Post #7 of 10 (1511 views)
Re: [Paul] Sorting by a different Variable [In reply to] Can't Post

If you read my post properly you will see this line...

"And remember, when you name functions after user input it can be dangerous, so untaint your input."

Jane.


Paul
Enthusiast

Aug 27, 2003, 3:03 PM

Post #8 of 10 (1496 views)
Re: [Jane] Sorting by a different Variable [In reply to] Can't Post

I read it properly first time and was emphasizing the need to validate user input =)


uri
Thaumaturge

Aug 27, 2003, 11:16 PM

Post #9 of 10 (1492 views)
Re: [Jane] Sorting by a different Variable [In reply to] Can't Post

that code has several problems, the biggest of which is that the sort comparison function is not warning safe. it uses <=> to compare strings and that spits out warnings.

also it is inefficient as you have multiple copies of the sort key and are doing a slice. using the schwartzian transform or the GRT will make this much faster and cleaner IMNSHO.

you can create a code reference to do the proper sort comparison if you know the type of each column.

for more on sorting read my sort paper


Jane
Novice

Aug 28, 2003, 2:27 AM

Post #10 of 10 (1489 views)
Re: [uri] Sorting by a different Variable [In reply to] Can't Post

Thank you uri. I've saved your paper and I'll read it sometime as you're obviously some 'sort' of guru Smile.

My appologies to you andy7t for posting bad code. Anyway, at least you now know that it can be done.

Cheers, Jane.

 
 


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

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