in reply to Iterating hash of blessed hashes

ron7: As BrowserUk suggests, your OPed code has a problem on the first pass through the loop because you are comparing against $rating, which is uninitialized on that occasion. (I'm not sure how you missed this warning. You did use warnings and strictures, didn't you?) However, initialization to zero will be a problem if a rating can ever be negative (a point on which your OP is silent) and all the ratings happen to be so, in which case you will get a bunch more warnings and also a wrong answer!

Replies are listed 'Best First'.
Re^2: Iterating hash of blessed hashes
by ron7 (Beadle) on Feb 21, 2011 at 05:02 UTC
    Didn't see any warning, but the code quickly evolved to this:
    my $hr = $tvdb->getSeriesBanners($title, 'season', 'seasonwide'); my $rating = 0; my $bwurl; my @keys = keys %$hr; foreach my $id (@keys) { my $hr2 = $hr->{$id}; if (($hr2->{Season} == $seasonnum) && ($hr2->{Rating} > $rating)) +{ $rating = $hr2->{Rating}; $bwurl = $hr2->{BannerPath}; } } if ($bwurl) { debug(1, "Using highest rated Series Banner\n"); return "http://thetvdb.com/banners/_cache/$bwurl"; }

    In which I now see a bug: If the only banner(s) available have a zero Rating, I end up with nothing. I'll change it to -1.

    The Rating is a 0..10 scale AFAIK (Thetvdb.com via TVDB::API), and I always use strict; use warnings;