I'm new to perl, and I've been stuck on this for a whole morning.
I have a hash and the keys are page names.
I have a for each loop that queries a database for each page in my hash. The query does a count() to get the number of occurences of this page.

I want to create another hash using the page names as the keys and the count() result as the values.

I'm almost there as this is the last part of my project, and its causing frustration now as I've spent a large part of the total time stuck on this stupid little bit.

At the moment its wrong as it puts the same value in all of the hash values, or something :)
Heres my code:
foreach $page(keys %yearly_page_hits){ $dbh = DBI->connect("DBI:mysql:$sqlDatabaseName:","$mysqlUsername"," +$mysqlPassword") or die "Connection Error: $DBI::errstr\n"; $sql = "select count(page) from counter_monthly where page='$page'"; $cursor = $dbh->prepare($sql); $cursor->execute or die "SQL Error: $DBI::errstr\n"; while (@row = $cursor->fetchrow_array) { print "while loop the row for $page is @row<br><br>\n"; $new_monthly_page_hits{$page}=@row; print "whileloop".$new_monthly_page_hits{$page}."<br><br>"; #push(@yearly_pages,@row); #sort(@yearly_pages); } }

Can anyone see what I'm doing wrong?
Thank you very much.

In reply to Hashing up hashes by pickledegg

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.