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

I'm fairly sure that I'm missing something extremely basic here but still getting to grips with Perl. I've been trying to run a select query from a database and then capture the results in a hash (which is fine):
while (my($word, $count) = $sth->fetchrow_array) { $tag{$word} = $count; }
Then I want to sort he results and insert them into some HTML and I keep getting a "Use of unitialized value in int":
foreach my $words (sort keys %tag) { my $fsize = $count; my $tag1 = $words => $word; printf "<style=\"font-size:%dpx;\">%s\n", int($fsize), $tag1; }
If I use my $fsize = $words =>$count, I get a use of unitialised value which refers to that line and it tries to put in the the key value (a word) into the place where the count goes (defined as int($fsize) in the output. I'm gradualy going spare with this and would appreciate some help and an explanation so that I can learn from it.

Replies are listed 'Best First'.
Re: Getting the value out a hash
by Fletch (Bishop) on May 01, 2008 at 13:21 UTC

    You need to read the docs for keys more carefully. It returns a list of, well, the keys. You then need to use those keys to extract the corresponding values out of your hash (my $fsize = $tag{ $word }).

    Also the => operator isn't magic, it's just a special comma that does quoting in some cases of the thing to its left. my $tag1 = $words => $word is just a long winded way of writing my $tag1 = $words, $word, and that simply means my $tag1 = $word which probably isn't going to do anything useful.

    Not to mention that there's really no reason to call int on a numeric value in this case (and if it were fractional running it through printf would truncate it to the whole part anyhow . . .).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Getting the value out a hash
by CountZero (Bishop) on May 01, 2008 at 13:40 UTC
    If you run your program under use strict; you get an error for my $fsize = $count; because $count was not pre-declared with my.

    You did say

    while (my($word, $count) = $sth->fetchrow_array) { $tag{$word} = $count; }
    but that lexical $count variable has gone out of scope when the while loop finished.

    Most probably you meant to say:

    foreach my $words (sort keys %tag) { my $fsize = $tag{$words}; printf "<style=\"font-size:%dpx;\">%s\n", $fsize, $words; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Getting the value out a hash
by mscharrer (Hermit) on May 01, 2008 at 13:23 UTC
    You should know that => is only a "fat" comma, so my $tag1 = $words => $word; is the same as my $tag1 = $words, $word;, which is pointless. Note that => forces string-context on the left argument so you can write NAME => $value instead of "NAME", $value.

    I think what you want is the following:

    foreach my $word (sort keys %tag) { my $fsize = $tag{$word}; printf "<style=\"font-size:%dpx;\">%s\n", int($fsize), $word; }

    PS: I would strongly recommend the use of:

    use strict; use warnings;
      Thanks for the explanations so I can learn for the future.
Re: Getting the value out a hash
by MelaOS (Beadle) on May 01, 2008 at 13:18 UTC
    hi, did you try to print out the $fsize var? i think it's complaining because your $fsize doesn't have any value. it's similar to most programming language where you try to work with null variables.