1  #!/usr/bin/perl -w
 2
 3  use strict;
 4  use CGI;
 5
 6  my $cgi = new CGI;
 7  my $how_col = $cgi->param('col') || 10;
 8  my $script = $cgi->script_name();
 9
10  my @ary = ( 1 .. 50 );
11  my $numb_elem = scalar(@ary);
12
13  @ary = sort { $a <=> $b } @ary;
14
15  my $how_rows = int($numb_elem / $how_col);
16  $how_rows += 1 if ( $numb_elem % $how_col );
17
18  print $cgi->header(), $cgi->start_html(
19      -title=>'Dynamically Sorted Data by Table Columns');
20
21  print qq(<CENTER><h4>Dynamically Sorted Data by Table Columns.</h4>
22          <TABLE border="1" cellpadding="3">);
23
24  for ( my $i = 0; $i < $how_rows; $i++ )
25  {
26      my @cols = ();
27      for ( 0 .. ($how_col - 1) ) {
28          push @cols, $i+$how_rows*$_;
29      }
30      print "<TR>";
31
32      for ( @cols )
33      {
34          if ( ! $ary[$_] ) {
35              print "<TD>&nbsp;</TD>";
36          }
37          else {
38              print "<TD>$ary[$_]</TD>";
39          }
40      }
41      print "</TR>";
42  }
43  print "</TABLE><BR>";
44  print qq(<FORM>
45          Enter Number of the Columns:
46          <SELECT name="col" onChange="columns(this)">);
47      for ( 1 .. 10 ) {
48          print qq(<OPTION value="$_"),
49              $_ == $how_col ? " SELECTED>" : ">",$_;
50      }
51  print "</SELECT></FORM></CENTER>";
52
53  print "<SCRIPT>
54      function columns(s) {
55          var v=s.options[s.selectedIndex].value;
56          location.href='$script?col='+v;
57      }
58      </SCRIPT>";
59  print $cgi->end_html();
60
61  exit(0);

See Output