On Thursday 14 August 2003 05:56, [EMAIL PROTECTED] wrote:
>
> Hi.  I'm still very much a Perl beginner, but I'm reading "Mastering
> Regular Expressions" by Friedl, so I expect to be able to at least
> help the group out with regexs when I'm a few more chapters in.  ;o)
>
> I've just created a script which, when run like this:
>
> tags.pl filename.html
>
> Produces results like this:
>
> #snipped for brevity
> <title>, 1 occurrances
> <td>, 81 occurrances
> </form>, 1 occurrances
> </tr>, 23 occurrances
> </font>, 10 occurrances
> </title>, 1 occurrances
> </strong>, 1 occurrances
> <applet>, 1 occurrances
> <script>, 6 occurrances
> <hr>, 2 occurrances
> <h3>, 1 occurrances
> <img>, 3 occurrances
> <h1>, 1 occurrances
> <body>, 1 occurrances
> </td>, 81 occurrances
> <head>, 1 occurrances
> </option>, 10 occurrances
>
> Is there an easy way to have this print out the matching opening and
> closing tags on one line:
> Example:  <td> 81, </td> 81

Yes there is.


use HTML::TokeParser;

my $p = HTML::TokeParser->new( 'index.html' );

my %data;
while ( my $token = $p->get_token() ) {
    next unless $token->[ 0 ] =~ /S|E/;
    $data{ $token->[ 1 ] }{ $token->[ 0 ] }++;
    }

for my $key ( sort keys %data ) {
    print "<$key> $data{$key}{S}, </$key> $data{$key}{E}\n";
    }

__END__


> If not, is there an easy way to sort the hash before printing, so
> that either it's sorted by the value,

Change the line:

for my $key ( sort keys %data ) {

To:

for my $key ( sort { $data{$a}{S} <=> $data{$b}{S} } keys %data ) {



John
-- 
use Perl;
program
fulfillment


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to