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.

In reply to Creating Tag Clouds from a Database by Quicksilver

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, 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, 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.