?
Solved

Sorting content of table within a html page Perl CGI or Perl script

Posted on 2005-04-06
15
Medium Priority
?
354 Views
Last Modified: 2013-12-25
Hi Experts

I have a content of html page looks like this

...some html stuff here
<table width="600"><p>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041211.131223/index.html">Smith, Martin. Masters Thesis, 1991</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041212.133427/index.html">Bush, J. W. (Joe Nogood). Masters Thesis, 1990</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041215.124610/index.html">Focker, Mo. Masters Thesis, 1992</a><p></td></tr>
heaps more
</TABLE></CENTER></BODY></HTML>

Could some one please help me on how i go about sorting the table by the surname so it looks like this, in a seprate Perl CGI or perl script...

...some html stuff here
<table width="600"><p>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041212.133427/index.html">Bush, J. W. (Joe Nogood). Masters Thesis, 1990</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041215.124610/index.html">Focker, Mo. Masters Thesis, 1992</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041211.131223/index.html">Smith, Martin. Masters Thesis, 1991</a><p></td></tr>
heaps more
</TABLE></CENTER></BODY></HTML>


This data is not comming from a databse so i can't achive this from that end...

Thanx in Advance
0
Comment
Question by:baz_can_fix_it
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 18

Assisted Solution

by:kandura
kandura earned 2000 total points
ID: 13722814
use HTML::TreeBuilder;

# parse the html that comes after the __DATA__ tag
my $tree = HTML::TreeBuilder->new_from_content(<DATA>);

# find the table you want to sort
my $table = $tree->look_down('_tag', 'table', 'width', 600);

# use a Schwartzian Transform to sort the tr's
my @tr = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, $_->as_trimmed_text] } $table->content_list;

# replace original tr's with the sorted ones
$table->splice_content(0, scalar(@tr), @tr);

# and dump the html out again
print $tree->as_HTML( '<>&', "\t" );

__DATA__
<html>
<body>
<center>
<table width="600"><p>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041211.131223/index.html">Smith, Martin. Masters Thesis, 1991</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041212.133427/index.html">Bush, J. W. (Joe Nogood). Masters Thesis, 1990</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041215.124610/index.html">Focker, Mo. Masters Thesis, 1992</a><p></td></tr>

</TABLE></CENTER></BODY></HTML>
0
 
LVL 18

Expert Comment

by:kandura
ID: 13722818
This is the output:

<html>
    <head>
    </head>
    <body>
        <center>
            <table width=600>
                <tr>
                    <td>
                        <p>
                    </td>
                </tr>
                <tr>
                    <td><font face="helvetica,arial"><a href="/public/dep-20041212.133427/index.html">Bush, J. W. (Joe Nogood). Masters Thesis, 1990</a><p></font></td>
                </tr>
                <tr>
                    <td><font face="helvetica,arial"><a href="/public/dep-20041215.124610/index.html">Focker, Mo. Masters Thesis, 1992</a><p></font></td>
                </tr>
                <tr>
                    <td><font face="helvetica,arial"><a href="/public/dep-20041211.131223/index.html">Smith, Martin. Masters Thesis, 1991</a><p></font></td>
                </tr>
            </table>
        </center>
    </body>
</html>
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 13722892
Hi kandura
Thanx for the quick responce

Any other ways without the use of HTML::TreeBuilder; Doesnt look like they have that library on the server

Can't locate HTML/TreeBuilder.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4
-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/s
ite_perl/5.005 .) at /../apache/cgi-bin/test.pl line 3.
BEGIN failed--compilation aborted at /../apache/cgi-bin/test.pl line 3.

Cheers
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
LVL 18

Expert Comment

by:kandura
ID: 13722931
Why don't you install it then? I really think this is about the shortest _and_ safest method.
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 13722980
I don't have rights on that server, i'll place a call with them, will take few days... I'll get back to you once installed ...

Thanx Heaps
0
 
LVL 18

Expert Comment

by:kandura
ID: 13722990
baz_can_fix_it,
> I don't have rights on that server,

You don't have shell access to that machine? that's a pity.

BTW, do you have to do the sorting on that server? Or could you do it on your own machine?

In any case, installing HTML::TreeBuilder will be a great addition to your toolbox. As you can see, it makes screen scraping and digging around in html a breeze :-)
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 13723012
Unfortunaly it has to be on that machine, the big problem we have is that machine is using perl for heaps of other imporatnt programs and we can't efford to mess around with the libraries...But they are going to see what they can do...Cheers
0
 
LVL 48

Expert Comment

by:Tintin
ID: 13724131
There's also the option of using Javascript to dynamically sort any of the columns in a table.  Depends on how much data you are displaying.
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 13724178
Hi Tintin

Java script would be great, that way i don't have to install anything on server side...but woyld need help with that as my javascripting is not that good...

Cheers
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13724522
do you mean to use a CGI to sort the ready (existing) html file?
Are you aware that using JavaScript also requires access to the server?
0
 
LVL 18

Accepted Solution

by:
kandura earned 2000 total points
ID: 13726465
ahoffmann,
> Are you aware that using JavaScript also requires access to the server?

What makes you say that?

All you need to do is serve the html in question with an added <script> section which accesses the DOM to sort the table rows:



<html>
<head>
<script>
addEvent(window, "load", sortables_init);

var SORT_COLUMN_INDEX;

function sortables_init() {
    // Find the table with width 600
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if ( thisTbl.width=600 ) {
            ts_resortTable(thisTbl);
        }
    }
}
function ts_getInnerText(el) {
    if (typeof el == "string") return el;
    if (typeof el == "undefined") { return el };
    if (el.innerText) return el.innerText;  //Not needed but it is faster
    var str = "";
   
    var cs = el.childNodes;
    var l = cs.length;
    for (var i = 0; i < l; i++) {
        switch (cs[i].nodeType) {
            case 1: //ELEMENT_NODE
                str += ts_getInnerText(cs[i]);
                break;
            case 3: //TEXT_NODE
                str += cs[i].nodeValue;
                break;
        }
    }
    return str;
}

function ts_resortTable(table) {
   
    // Work out a type for the column
    if (table.rows.length <= 1) return;

    sortfn = ts_sort_caseinsensitive;
    SORT_COLUMN_INDEX = 0;

    var newRows = new Array();
    for (j=0;j<table.rows.length;j++) { newRows[j] = table.rows[j]; }

    newRows.sort(sortfn);

    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    for (i=1;i<newRows.length;i++) { table.tBodies[0].appendChild(newRows[i]);}
}

function getParent(el, pTagName) {
    if (el == null) return null;
    else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())    // Gecko bug, supposed to be uppercase
        return el;
    else
        return getParent(el.parentNode, pTagName);
}

function ts_sort_caseinsensitive(a,b) {
    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}
</script>
</head>
<body>
<center>
<table width="600"><p>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041211.131223/index.html">Smith, Martin. Masters Thesis, 1991</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041212.133427/index.html">Bush, J. W. (Joe Nogood). Masters Thesis, 1990</a><p></td></tr>
<tr><td><font face=helvetica,arial><a href="/public/dep-20041215.124610/index.html">Focker, Mo. Masters Thesis, 1992</a><p></td></tr>

</TABLE></CENTER></BODY></HTML>
0
 
LVL 18

Expert Comment

by:kandura
ID: 13726537
I would like to note that, although this javascript should work in most browsers, not everyone has javascript enabled. (I've tested in IE6 and Firefox).
(Much) older browsers might not work either.
All of those issues would be taken care of when you sort on the server prior to outputting the document.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13728457
> All you need to do is serve the html in question with an added <script> section ...
and how do you do that without access to the server?

anyway, nice script ;-)
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 13732195
kandura YOU ARE A LEGEND!!!!

Excellent answer, very happy with both your answers, but your JavaScript one works without any problems...Cheers

much appreciated

Thank you
0
 
LVL 18

Expert Comment

by:kandura
ID: 13732209
Thanks for the huge compliment :-)

Good luck with your app!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question