ron7 has asked for the wisdom of the Perl Monks concerning the following question:
A function call is returning a struct that Data::Dumper says looks like this (I've elided some entries):
I want to iterate over the entries and extract the BannerPath with the highest Rating for a given Season. What's the best approach? My solution feels a bit clunky...{ + '771271' => bless( { + 'Language' => 'en', + 'Rating' => '10.0000', + 'BannerType2' => 'seasonwide', + 'Season' => '1', + 'BannerPath' => 'seasonswide/79349-1-3. +jpg', 'BannerType' => 'season', + 'RatingCount' => '1' + }, 'DBM::Deep::Hash' ), + '586831' => bless( { + 'Language' => 'en', + 'Rating' => '5.5000', + 'BannerType2' => 'seasonwide', + 'Season' => '4', + 'BannerPath' => 'seasonswide/79349-4-3. +jpg', 'BannerType' => 'season', + 'RatingCount' => '2' + }, 'DBM::Deep::Hash' ), + '771301' => bless( { 'Language' => 'en', 'Rating' => '10.0000', 'BannerType2' => 'seasonwide', 'Season' => '4', 'BannerPath' => 'seasonswide/79349-4.jp +g', 'BannerType' => 'season', 'RatingCount' => '1' }, 'DBM::Deep::Hash' ) };
my $hdata = <call that creates the struct>; my ($rating, $bwurl); my @keys = keys %$hdata; foreach my $id (@keys) { my $hr2 = $hdata->{$id}; if (($hr2->{Season} == $seasonnum) && ($hr2->{Rating} > $rating)) +{ $rating = $hr2->{Rating}; $bwurl = $hr2->{BannerPath}; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Iterating hash of blessed hashes
by BrowserUk (Patriarch) on Feb 17, 2011 at 05:00 UTC | |
|
Re: Iterating hash of blessed hashes
by ikegami (Patriarch) on Feb 17, 2011 at 06:38 UTC | |
by BrowserUk (Patriarch) on Feb 17, 2011 at 09:13 UTC | |
by ikegami (Patriarch) on Feb 17, 2011 at 16:25 UTC | |
|
Re: Iterating hash of blessed hashes
by AnomalousMonk (Archbishop) on Feb 17, 2011 at 08:29 UTC | |
by ron7 (Beadle) on Feb 21, 2011 at 05:02 UTC | |
|
Re: Iterating hash of blessed hashes
by Anonymous Monk on Feb 17, 2011 at 05:20 UTC | |
by ron7 (Beadle) on Feb 17, 2011 at 06:18 UTC | |
by Anonymous Monk on Feb 17, 2011 at 07:18 UTC | |
|
Re: Iterating hash of blessed hashes
by Anonymous Monk on Feb 17, 2011 at 04:50 UTC |