Mathew,

try to test every condition going INTO the hash (or hashes) before you actually assign a value to a key. as mentioned earlier, hashes must be key/value pairs. the key will auto-vivify if a key is "new" but only if a corresponding value accompanies it. when you assign values to all of your hashes, check to see if that value is *defined*. when you try to assign undefined values into a hash, you will get undesirable results. you already know this, but just presenting it for the sake of completeness... you need to ensure all of your KEYS are unique (unless of course you want to overwrite the previous entry with the most recent... last in, and all that :) ...

so now you have keys, and when you assign values to them, you need to make sure it is defined, else store some arbitrary value in there that you can test for later... like a scalar "empty" or something else that is easily determined to NOT be useful data.

are you returning a hash from a method in another module? if so, try to dereference it as soon as it is returned, storing it into a new lexical hash and iterate through the lexical hash...

hope some of that helps.

i would include some code snips, but i am in the middle of a construction project, and am pressed for time.

cheers

~CK


Mathew Snyder wrote:
Rob Dixon wrote:
It's very unclear what you're trying to do, and what your RT package
does. Let
me make some observations and guesses and you can tell me where I'm
right or
wrong.

$tix is an iterator that will return a sequence of tickets through the Next
method.

$ticket is a data handle that will return an id, an Environment, and a
Transactions iterator. The environment appears to be a customer number.

$transaction is a RT handle that will return a TimeTaken and a Creator. The
creator appears to be a user identity.

What you want to generate is a hash of total time worked by each user
across
all customers and tickets.

The code below may help. Clearly it is untested as I have no RT package or
database, but it is something we can discuss. Please let us know how close
this is to what you are aiming for.

HTH,

Rob


use warnings;
use strict;

use lib '/usr/local/rt-3.6.3/lib';
use lib '/usr/local/rt-3.6.3/local/lib';
use RT;
use RT::Tickets;
use RT::Users;

RT::LoadConfig();
RT::Init();

my $tix = new RT::Tickets($RT::SystemUser);
$tix->FromSQL('Queue = "CustomerCare" OR Status = "resolved" OR Status =
"open"');

my %timeworked;

while (my $ticket = $tix->Next) {

 my $id = $ticket->id;
 my $customer = $ticket->FirstCustomFieldValue('Environment');

 unless ($customer) {
   warn "warning $id no environment";
   next
 }

 my $transactions = $ticket->Transactions;

 while (my $transaction = $transactions->Next) {
my $user = $transaction->Creator;
   my $time = $transaction->TimeTaken;
      $timeworked{$user} += $time;
 }
}

foreach my $user (keys %timeworked) {
 printf "%s -> %s\n", $user, $timeworked{$user};
}


Rob,

You are correct in all of your assumptions above.  Your code example spurred me
to get a bit further with my script.  I now have this:

#!/usr/bin/perl

use warnings;
use strict;
use lib '/usr/local/rt-3.6.3/lib';
use lib '/usr/local/rt-3.6.3/local/lib';
use RT;
use RT::Tickets;
use RT::Users;

RT::LoadConfig();
RT::Init();

my $tix = new RT::Tickets(RT::SystemUser);
$tix->FromSQL('Queue = "CustomerCare" AND Status = "open" AND Created <
"2007-03-03"');

my %environment;
my %timeworked;
my $users = new RT::Users(RT::SystemUser);
$users->LimitToPrivileged;

while (my $ticket = $tix->Next) {
        my $environment = $ticket->FirstCustomFieldValue('Environment');

        unless ($environment) {
                warn "warning" . $ticket->id. "no environment";
                next
        }

        my $transactions = $ticket->Transactions;

        while (my $transaction = $transactions->Next) {
                next unless ($transaction->TimeTaken);

                foreach my $user ($users->Next) {
                        if ($user = $transaction->Creator) {
                                $timeworked{$user} += $transaction->TimeTaken;
                                $environment{$environment} = $timeworked{$user};
                        }
                }
        }
}

foreach my $env (sort keys %environment) {
        print "\n" . $env . "\n";
        print "--------------------\n";
        foreach my $user (sort keys %timeworked) {
                print $user . " -> " . $timeworked{$user} . "\n";
        }
}

This is getting closer to what I want.  However, the %timeworked hash still
doesn't do what I'm looking for.  Rather than only totalling up the time worked
on each customer for each user and then adding that to the customer environment
hash as $environment{$env}{$user} it is still iterating through every ticket and
transaction and adding that time to $timeworked{$user} which is then output for
each customer at the end.  To clarify, each $environment{$env} has an identical
%timeworked hash printed with it.  That %timeworked hash is a total for all time
for each user and not just the time for the customer.

I've tried so many different alterations but can't figure out how to get the
%timeworked hash populated the way I need and paired with the %environment hash
properly.

Your further assistance will be greatly appreciated.

Mathew



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


Reply via email to