I'm trying to build a tag cloud derived in terms in a database as an exercise but I've found myself out of my depth in terms of linking the pieces of code together to obtain the top 200 words (there are over 20,000 so I'm looking to cut it down a little). I'd be grateful for some help in finding a solution so that I can learn where I've gone awry.

#!c:\perl\bin\perl.exe use strict; use warnings; use DBI; my $count; my $tagCnt; my $logRange; my $fsize; my $k; my @tagfiledb; my @sortkeys; my $word; my $occurrence; my $tags; my %dbtags; my $dbh = DBI->connect('dbi:mysql:milton:localhost', 'user', 'pword'); my $sth = $dbh->prepare ('SELECT word, occurrences FROM statistic'); #load in tag file - do this from db my $tagfile = shift; $sth->execute; while (($word, $occurrence) = $sth->fetchrow_array ){ %dbtags = ($word, $count); } $sth->finish; $dbh->disconnect(); $tags= @tagfiledb; my $useLogCurve = 1; my $minFontSize = 10; my $maxFontSize = 36; my $fontRange = $maxFontSize - $minFontSize; #filter the script to top 200 tags my $maxtags = 200; @sortkeys = sort {$tags->{$b}->{count}<=> $tags->{$a}->{$count}} keys %{$tagfile}; @sortkeys = splice @sortkeys, 0, $maxtags; #determine counts my $maxTagCnt = 1; my $minTagCnt = 10000000; foreach $k (@sortkeys) { $maxTagCnt = $tags->{$k}->{count} if $tags->{$k}->{count} > $maxTagCnt; $minTagCnt = $tags->{$k}->{count} if $tags->{$k}->{count} > $minTagCnt; } my $minLog = log($minTagCnt); my $maxLog = log($maxTagCnt); my $logrange = $maxLog - $minLog; $logrange = 1 if ($maxLog - $minLog); sub DetermineFontSize ($) { my ($tagCont) = @_; my $cntRatio; if ($useLogCurve) { $cntRatio = log($tagCnt)-$minLog/$logRange; } else { $cntRatio = ($tagCnt-$minTagCnt)/($maxTagCnt-$minTagCnt); } $fsize = $minFontSize + $fontRange * $cntRatio; return $fsize; } #output tag cloud print "Content-type: text/html\n"; print<<EOT; <html> <head> <link href="/css/tagcloud.css" rel="stylesheet" type="text/css"> </head> <body> <div class=\"cdiv\"> <p class=\"cbox\"> EOT #output the keys foreach $k(sort @sortkeys){ $fsize = DetermineFontSize($tags->{$k}->{$occurrence}); my $tag = $tags->{$k}->{$word}; printf int($fsize), $tag; } #output end of tag file print <<EOT </p> </div> </body> </html> EOT __END__


In reply to Building a tag cloud from a database by Quicksilver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.