On 3/28/12 Wed  Mar 28, 2012  10:02 AM, "timothy adigun"
<2teezp...@gmail.com> scribbled:

> Hi Chris,
> 
> On Wed, Mar 28, 2012 at 12:02 PM, Chris Stinemetz
> <chrisstinem...@gmail.com>wrote:
> 
>>> 

>> foreach my $cell ( @wanted ) {
>>   print  "$cell:";
>>   foreach my $hr ( @hours ) {
>> 
>        foreach(keys %{$href->{$cell}}){
>             print "\t",$href->{$cell}{$_} if $_ == $hr;
>        }

That is a very inefficient way to fetch a value with a specific key from a
hash. You are iterating over all of the keys of the hash looking for the
specific key. It is much more efficient to just use the 'exists' operator to
check if the key exists in the hash, or the 'defined' operator to check if a
value is defined for that key in the hash. For example (untested):

for my $cell ( @wanted ) {
  print "$cell:";
  my $hr_ref = $href->{$cell};
  for my $hr ( @hours ) {
    print "\t" . (defined $hr_ref->{$hr} ? $hr_ref->{$hr} : '');
  }
  print "\n";
}




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to