Mathew Snyder wrote:

[EMAIL PROTECTED] wrote:

Mathew Snyder wrote:

A subroutine I'm working on takes two hash references.  The hashes are each
actually a HoH.

timesheet(\%opsTotal, \%opsEnvTotal);

The problem I'm having is that I need to look past the first hash and into the
second for the existence of a particular key.  I'm not sure how to go about
doing this.

sub timesheet {
  my ($dept, $env) = @_;

#This is where I need help.  'user' is in the second hash but I'm not sure how
#to get past the first one.  Should I use a foreach and step through each key?
  if (exists $dept->{user}) {
    open TIMESHEET, ">/work_reports/user/ops_timesheet_weekof_$endDate.txt";
  }else{
    open TIMESHEET, ">/work_reports/user/eng_timesheet_weekof_$endDate.txt";
  }

  print TIMESHEET "Timesheet for $startDate to $endDate\n\n\n";

  foreach my $environ (sort keys %$dept) {
    #Print the header for our data
    print TIMESHEET "$environ", "\n";
    printf TIMESHEET "%10s%8s\n", "User", "hh:mm";
    print TIMESHEET ("-" x 30);
    print TIMESHEET "\n";
    foreach my $name (sort keys %{ $dept->{$environ} }) {
      printf TIMESHEET "%10s%8s\n", "$name", "$dept->{$environ}->{$name}";
    }
    printf TIMESHEET ("-" x 30);
    print  TIMESHEET "\n";
    printf TIMESHEET "%18s\n\n", "$env->{$environ}";
  }
  close TIMESHEET;
}

I am not sure that I understand your problem.
In General if you want to check the existence of the key "user" in the first 
hash, you can use the following
 if ($dept and exists($dept->{user}){ ....}else{....}

Hope that helps

To make this even more tricky, if the user shows up in even one of the second
hashes the whole %$dept hash is affected.

We have $dept{customer}{user}
        $dept{customer}{user1}
        $dept{customer1}{user}
  $dept{customer1{user1}
  $dept{customer2}{user}

So, even though user2 isn't found in either customer's or customer1's hashes,
its existence in customer2's hash means that all work will be done on the
preceding two hashes as well.

Hi Mathew

(Please bottom-post your replies, so that extended threads can remain 
comprehensible.
Thanks.)

I think you have a less-than optimal solution at present. You need to express 
the
problem more clearly and, once that is done, a solution will present itself. You
mention the user2 key, but there is no such key in your data and I am not at all
sure what your criteria are.

Here's how I understand your data. In $dept you have a reference to a hash with 
a
number of customer names as keys and further hash references as values. You 
need to
check whether a particular user exists as a key to any of the secondary hashes. 
Am
I close?

If you loop over all the values of the %$dept hash, you will be able to check
whether the user exists in any of the secondary hashes, like this:

 my $user;
 foreach my $data (values %$dept) {
   $user++ if exists $data-{user};
 }

and now you can write

 if ($user) {
   :
 }

is that close to what you need?

The foreach loop can be written more concisely as

 my $user = grep exists $_->{user}, values %$dept;

Please let us now how your problem deviates from this guess.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to