Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting by column on webpage 1

Status
Not open for further replies.

yesti

MIS
Dec 8, 2000
166
US
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 thanks to code from KevinADC.

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:

Code:
my $sortvar = param('sort');

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:

Code:
if ($sortvar == '1')
{
my @records = map  { $_->[0] }
             sort { $b->[1] cmp $a->[1] }  
             map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                   my ($d,$m,$y) = split(/\s+/,$date);
                   $d = $d<10 ? "0$d" : $d;
                   $m = $month{lc substr $m,0,3};
                   [$_,"$y$m$d"]} @records;
}
elsif ($sortvar == '2')
{
my @records = map  { $_->[0] }
             sort { $a->[1] cmp $b->[1] }  
             map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                   my ($d,$m,$y) = split(/\s+/,$date);
                   $d = $d<10 ? "0$d" : $d;
                   $m = $month{lc substr $m,0,3};
                   [$_,"$y$m$d"]} @records;
}

Any ideas?
 
@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.

Code:
my @sorted;#<--scoped globally
if ($sortvar == 1)
{
@sorted = map  { $_->[0] }
             sort { $b->[1] cmp $a->[1] }  
             map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                   my ($d,$m,$y) = split(/\s+/,$date);
                   $d = $d<10 ? "0$d" : $d;
                   $m = $month{lc substr $m,0,3};
                   [$_,"$y$m$d"]} @records;
}
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. 
@sorted = map  { $_->[0] }
             sort { $a->[1] cmp $b->[1] }  
             map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                   my ($d,$m,$y) = split(/\s+/,$date);
                   $d = $d<10 ? "0$d" : $d;
                   $m = $month{lc substr $m,0,3};
                   [$_,"$y$m$d"]} @records;
}
print "$_\n" for @sorted;




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Boy that was a stupid mistake on my part, thanks. @records is declared globally waaay above in the code and I guess Perl doesn't care too much if I declare it again? (I took out the if statement to verify the sorting was doing something)
 
if you have warnings on perl will let you know if you are over-riding the same variable by declaring it with "my" again within the same scope. Normally that is something you want to avoid.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top