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: Beginner:
Sorting entry's in html file

 



ad65
Deleted

May 18, 2001, 9:33 AM

Post #1 of 3 (887 views)
Sorting entry's in html file Can't Post

Hi, I need to know how to sort some lines in a html file into alphabetical order. In order to recognise which lines to sort, the lines begin with (<)!--mailto:--(>). After this is the names in a similar enclosure(hidden, I have to replace < and > with (<) and (>) or it doesn't show up in here), in which I want to sort by. The html file contains, for example this is an extract from the html file:

<P>
<table width=95% cellspacing=1 cellpadding=0 align=center bgcolor=#aa0000>
<tr>
<td align=center><font face=tahoma size=2 color=#dfdfdf><b>Codes</td>
<td align=center><font face=tahoma size=2 color=#dfdfdf><b>Author</td>
<td align=center><font face=tahoma size=2 color=#dfdfdf><b>Date</td>
</tr>
(<)!--Next Alpha Here:--(>)
(<)!--mailto:--(>)(<)!--Peter--(>)><TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New1.html">New1</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Peter@mydomain.com">Peter</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Frank--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New2.html">New2</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Frank@mydomain.com">Frank</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Tim--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New3.html">New3</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Tim@mydomain.com">Tim</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Wayne--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New4.html">New4</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Adam@mydomain.com">Adam</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Adam--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New5.html">New5</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Wayne@mydomain.com">Wayne</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Fiona--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New6.html">New6</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Fiona@mydomain.com">Fiona</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
(<)!--mailto:--(>)(<)!--Barbara--(>)<TR><TD BGCOLOR=DFDFDF WIDTH=50%><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="../news/New7.html">New7</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2 COLOR=aa0000><A HREF="mailto:Barbara@mydomain.com">Barbara</A></FONT></TD><TD ALIGN=CENTER BGCOLOR=DFDFDF><FONT FACE=TAHOMA SIZE=2>May 18 2001</FONT></TD></TR>
</table>

I'm writing something similar to a guestbook script, so the new entry's go in at the top there. Once this is done, I want to arrange them all in alphabetical order. As you can see, there are html lines that I don't want to change at all. Just the lines that start with (<)!--mailto:--(>) I want to sort in alphabetical order by what comes next inside (<)!-- --(>). This is probably a crap explanation, but if anyone knows how to do this then please help.

Sorry if the code is a bit messy, each line that starts(<)!--mailto:--(>) is supposed to be one whole line right down to the next mailto

(This post was edited by ad65 on May 18, 2001, 8:38 AM)


mhx
Enthusiast / Moderator

May 18, 2001, 2:43 PM

Post #2 of 3 (877 views)
Re: Sorting entry's in html file [In reply to] Can't Post

Hi!

First of all, this is a nice description of your problem. A lot better than the one in the last thread. Smile

I think the solution should be just as easy. Try this, it worked perfectly on my machine.


Code
#!/bin/perl -w 
use strict;

my $file = "xxx";
my $pat = "<!--mailto:-->";

open FILE, $file or die "cannot open '$file': $!\n";
my @lines = <FILE>;
close FILE;

my @sorted = sort grep { /^$pat/ } @lines;

open FILE, ">$file" or die "cannot open '$file': $!\n";
print FILE map { /^$pat/ ? shift(@sorted) : $_ } @lines;
close FILE;

BTW: Why don't you generate the HTML code dynamically? That's what Perl/CGI is for! You could store only the data (names, links) in a file, read that file in and sort it, and then generate the HTML on the fly. If you have any questions concering how to do this, feel free to ask!

-- Marcus



ad65
Deleted

May 19, 2001, 4:42 AM

Post #3 of 3 (868 views)
Re: Sorting entry's in html file [In reply to] Can't Post

Hey, thats great it works fine thankyou. I think i'm going to generate the HTML code dynamically next time I make a script, it just seemed a bit confusing to do cause i'm new at this. I've actually made/almost made a script, thanx to certain helpers in here, that I think was too hard since it was my first, but i've learnt quite a lot, and i'll mess about in the next version of it, thanx for the advice and help once again. :)


 
 


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

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