I'm currently trying to build a tag cloud driven by a database to start experimenting with different ways of text mining. I've been using the O'Reilly PDF code as a basis and have swapped their use of a file to my DB connection but the code keeps falling over with the foreach loop - I keep getting syntax error on $k( but no useful guidelines on what the error is. Is this to do with the way that I've used EOT?
#!c:\perl\bin\perl.exe
use strict;
use warnings;
use DBI;
my $count;
my $tagCnt;
my $logRange;
my $fsize;
my $k;
my $tags;
my $dbh = DBI->connect('dbi:mysql:milton:localhost', 'user', 'pword');
my $sth = $dbh->prepare ('SELECT * FROM statistics');
#load in tag file - do this from db
my $tagfile = shift;
$sth->execute;
my @result = $sth->fetchrow_array;
$tagfile = join("", @result) or die "Nothing loaded from db\n";
$dbh->disconnect();
my $useLogCurve = 1;
my $minFontSize = 10;
my $maxFontSize = 36;
my $fontRange = $maxFontSize - $minFontSize;
my $maxtags = 200;
my @sortkeys = sort {$tags->{count}<=> $tags->{$a}->{$count}} keys
%{$mytags::tags};
@sortkeys = splice @sortkeys, 0, $maxtags;
#determine counts
my $maxTagCnt = 0;
my $minTagCnt = 10000000;
foreach my $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 <<EOT
<html>
<head>
<link href="mystyle.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}->{count});
my $tag = $tags->{$k}->{tag};
printf int($fsize), $tag;
}
#output end of tag file
print <<EOT
</p>
</div>
</body>
</html>
EOT
I'm trying to get this running so that I can begin to debug anything else that isn't working in it.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|