Mathew Snyder wrote:

Rob Dixon wrote:

Mathew Snyder wrote:

I have a problem printing out a hash.  This is the script I'm working
with:
[snip old code]

When I print the hash all I get is a reference. I can't seem to figure out how to get the actual contents. I've tried using \$,
%$, %{$env} and %{env}.  Some cause errors and some simply print
out the reference.  I'm not even sure I'm doing the
"$timeworked = " line correctly.  Can someone help me out with this
please?

Nothing is ever put into the %env hash. If it had contents you could
display them using

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

but as it stands this will print nothing. I can't even work out what you
expect to be in there, so I can't help you with the $timeworked assigment.
Tell us a little bit more please.

Essentially, this is a reporting script for our trouble ticket system. Each
ticket has several transactions associated with it that actually make up
the ticket.  Some of those transactions are the addition of time spent on
working on the ticket by its owner.

What I am trying to do is create a hash of customers which will contain a
hash of each user and the total time spent on the customer.  First,
creating the customer hash and then, for each customer, look at its tickets
and keep a running total of time spent on the customer for each user.

I reworked it and about 90% of it works.

#!/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" OR Status = "resolved" OR Status = 
"open"');

my %timeworked;

while (my $ticket = $tix->Next) {
        my $customer = $ticket->FirstCustomFieldValue('Environment');
        unless ($customer) {warn "warning" . $ticket->id. "no environment"; 
next}
        my %customer = ${customer};
        my $transactions = $ticket->Transactions;
        while (my $transaction = $transactions->Next) {
                next unless ($transaction->TimeTaken);

                my %env = $transaction->Creator;
                my %env = ${env};

                foreach my $cust (keys %customer) {
                        foreach my $user (keys %env) {
                                $env{$user} = $transaction->TimeTaken;
                                foreach my $time ( keys %{$env{$user}} ) {
                                        $timeworked{$cust} = $env{$user}{$time};
                                }
                        }
                }

                foreach my $key (keys %env) {
                        print "Working on " . $ticket->id . " for $customer" . " -> " . 
$key . "\n";
                        foreach my $subkey (keys %timeworked) {
                                print "Time worked for " . $subkey . " is " . 
$timeworked{$subkey} . "\n";
                        }
                }
        }
}

What I get now is the following:
[Wed Mar 21 08:11:12 2007] [crit]: Can't use string ("5") as a HASH ref while 
"strict refs" in use at ./user_timesheet_new.pl line 34. 
(/usr/local/rt-3.6.3/lib/RT.pm:346)
Can't use string ("5") as a HASH ref while "strict refs" in use at 
./user_timesheet_new.pl line 34.

I'm guessing "5" is a bit of data that is being pulled into the hash though
I don't know for certain.  I have no way of telling since rolling back the
change I made to get to this point would leave me with nothing in that
column anyway.

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};
}

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


Reply via email to