[rt-users] Put custom field objects into a hash

Mathew Snyder theillien at yahoo.com
Sun Mar 25 03:22:32 EDT 2007


I'm creating an object comprised of a customfield using my $customfield =
$ticket->FirstCustomFieldValue('CustomField');

What I would like to do is iterate through the contents of that object and place
each one in a hash initializing its value to 0.  I've tried to set the object
reference to a hash and then iterate through it setting the value to 0 but later
on, I try to use the hash and it gives me an error telling me "Can't use string
("0") as a HASH ref while "strict refs" in use at ./user_timesheet_3.pl line 62."

This is my script:

#!/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();

# Declare our global variables
my (@days, @months, @years, @date);
my $time = time();

for (1 .. 7) {
  $time -= 24*60*60;
  @date = (localtime($time))[3 .. 5];
  push @days, (sprintf '%02d', $date[0]);
  push @months,(sprintf '%02d',$date[1] + 1);
  push @years, $date[2] + 1900;
}
my $endDate     = join "-", $years[0], $months[0], $days[0];
my $startDate   = join "-", $years[$#years], $months[$#months], $years[$#years];

my $tix = new RT::Tickets(RT::SystemUser);
$tix->FromSQL('Queue = "CustomerCare" AND Status = "open" AND Created <
"2007-03-03"');
#$tix->FromSQL('Queue = "CustomerCare" AND (Status = "resolved" OR Status =
"open") AND (LastUpdated >="' .  $startDate . '" AND LastUpdated < "' .
$endDate. '")');

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

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

        foreach my $key (keys %environment) {
                print $key . "\n";
        }

        my $transactions = $ticket->Transactions;
       foreach my $enviro (keys %environment) {
               while ($transaction = $transactions->Next) {
                       next unless (($transaction->TimeTaken) && ($enviro ==
$ticket->FirstCustomFieldValue('Environment'))) ;
                       print "Working on " . $ticket . " for " . $enviro . "\n";
                       $timeworked{$transaction->Creator} +=
$transaction->TimeTaken;
               }
       }

       foreach my $user (keys %timeworked) {
               foreach my $cust (keys %environment) {
                       $environment{$cust}{$user} = $timeworked{$user};
               }
       }
}


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


The error is occurring at the end where the print $env . " -> " .
$environment{$env}{$user} . "\n"; line is.

Can anyone help me by explaining how to populate a hash using each custom field
object as a key?

Mathew



More information about the rt-users mailing list