Thank you. How would I return the values along with the keys that meet the criteria from the sub routine?
Thanks! Chris On Mar 25, 2012 10:04 AM, "Rob Dixon" <rob.di...@gmx.com> wrote: > On 25/03/2012 14:11, Chris Stinemetz wrote: > >> Below is snippet from Data::Dumper dump from a hash I have. >> >> What is the best approach for only printing the hashes that have the >> value 'ND' or hashes that have different values such as '149' does >> below. >> >> '149' => { >> '05' => '1', >> '06' => '1', >> '00' => '1', >> '01' => '2', >> '02' => '2', >> '03' => '2', >> '04' => '2' >> }, >> '077' => { >> '05' => 'ND', >> '06' => 'ND', >> '00' => 'ND', >> '01' => 'ND', >> '02' => 'ND', >> '03' => 'ND', >> '04' => 'ND' >> }, >> '078' => { >> '05' => '1', >> '06' => '1', >> '00' => '1', >> '01' => '1', >> '02' => '1', >> '03' => '1', >> '04' => '1' >> }, >> >> > Hi Chris > > The program below puts the keys of the hash elements that pass your > criteria into @wanted. > > Hope it helps, > > Rob > > > use strict; > use warnings; > > my $href = { > '149' => { > '05' => '1', > '06' => '1', > '00' => '1', > '01' => '2', > '02' => '2', > '03' => '2', > '04' => '2' > }, > '077' => { > '05' => 'ND', > '06' => 'ND', > '00' => 'ND', > '01' => 'ND', > '02' => 'ND', > '03' => 'ND', > '04' => 'ND' > }, > '078' => { > '05' => '1', > '06' => '1', > '00' => '1', > '01' => '1', > '02' => '1', > '03' => '1', > '04' => '1' > }, > }; > > sub ND_or_multiple { > my %uniq; > for (values %{$href->{$_[0]}}) { > return 1 if $_ eq 'ND'; > $uniq{$_}++; > return 1 if keys %uniq > 1; > } > return ''; > } > > my @wanted = grep ND_or_multiple($_), keys %$href; > > print map "$_\n", @wanted; > > **OUTPUT** > > 149 > 077 >