By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Sorting by column on webpage

P: 4
I have very little experience with perl. I have a plx/tmpl page that displays a data table based on a @records array. I am able to sort the array by a subkey.

I want to create links out of the column headers then change the sort order based on what the user clicks (ascending/descending). The links change the 'sort' querystring value. I am able to get the querystring with:

Expand|Select|Wrap|Line Numbers
  1. my $sortvar = param('sort');
  2.  
I coded the tmpl file to change the link when the user clicks it, that part works fine.
I tried this in my plx page but it does not do anything:

Expand|Select|Wrap|Line Numbers
  1. if ($sortvar == '1')
  2. {
  3. my @records = map  { $_->[0] }
  4.              sort { $b->[1] cmp $a->[1] }  
  5.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  6.                    my ($d,$m,$y) = split(/\s+/,$date);
  7.                    $d = $d<10 ? "0$d" : $d;
  8.                    $m = $month{lc substr $m,0,3};
  9.                    [$_,"$y$m$d"]} @records;
  10. }
  11. elsif ($sortvar == '2')
  12. {
  13. my @records = map  { $_->[0] }
  14.              sort { $a->[1] cmp $b->[1] }  
  15.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  16.                    my ($d,$m,$y) = split(/\s+/,$date);
  17.                    $d = $d<10 ? "0$d" : $d;
  18.                    $m = $month{lc substr $m,0,3};
  19.                    [$_,"$y$m$d"]} @records;
  20. }
  21.  
Any ideas?
Feb 5 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
Giving up on Tek-Tips? ;)
Feb 5 '08 #2

P: 4
Giving up on Tek-Tips? ;)
The tek-tips site was giving me errors earlier, so I had to search for a few more forums. I'm starting to really not like perl :(
Feb 5 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
The tek-tips site was giving me errors earlier, so I had to search for a few more forums. I'm starting to really not like perl :(

Yea, Tek-Tips does that more than most websites, errors that is.

Not liking perl? May a foul wind blow in your general direction! ;)
Feb 5 '08 #4

P: 4
LOL. Well many thanks to people like you that are making the experience a little better.

I see you replied to my topic on tek-tips. Waiting for that site to respond is killing me ;-P
Feb 5 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
LOL. Well many thanks to people like you that are making the experience a little better.

I see you replied to my topic on tek-tips. Waiting for that site to respond is killing me ;-P
In case you can't open TT:

@records is scoped wrong. When you use "my", which you should, the variables is only visible to the enclosing block, in your case the if{} elsif{} blocks. Also you don't need to quote a number to match it numerically.

Expand|Select|Wrap|Line Numbers
  1. my @sorted;#<--scoped globally
  2. if ($sortvar == 1)
  3. {
  4. @sorted = map  { $_->[0] }
  5.              sort { $b->[1] cmp $a->[1] }  
  6.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  7.                    my ($d,$m,$y) = split(/\s+/,$date);
  8.                    $d = $d<10 ? "0$d" : $d;
  9.                    $m = $month{lc substr $m,0,3};
  10.                    [$_,"$y$m$d"]} @records;
  11. }
  12. else {#<-- you don't really need to check the value of $sortvar here becuase if it is not 1 then this is the default sort.
  13. @sorted = map  { $_->[0] }
  14.              sort { $a->[1] cmp $b->[1] }  
  15.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  16.                    my ($d,$m,$y) = split(/\s+/,$date);
  17.                    $d = $d<10 ? "0$d" : $d;
  18.                    $m = $month{lc substr $m,0,3};
  19.                    [$_,"$y$m$d"]} @records;
  20. }
  21. print "$_\n" for @sorted; 
Feb 6 '08 #6

P: 4
TT just came up before I posted this, thanks!
Feb 6 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.