Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Creating Tag Clouds from a Database
by FunkyMonk (Chancellor) on Apr 24, 2008 at 10:15 UTC
    You're missing semicolons. Your prints should look like...
    print <<EOT; </p> </div> </body> </html> EOT

    Unless I state otherwise, my code all runs with strict and warnings
Re: Creating Tag Clouds from a Database
by stiller (Friar) on Apr 24, 2008 at 10:22 UTC
    If you insert a line

    on the line before the line perl complains about, and then try to move it (the __END__ line) up one line at a time until perl stops complaining, then down one line again, you have found the first offending line.

    The line

    my @sortkeys = sort {$tags->{count}<=> $tags->{$a}->{$count}} keys %{$ +mytags::tags};
    has several problems, but I guess you can see them now?