Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting variables while building a hash 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Hello all,

This thread relates to a thread I started a week or so ago about outputing to a file that KevinADC helped me with. The thread seems to have been deleted, so I will give the details. I have the results of two DB queries in arrays. What I want to do is loop through them, combine them into one into one data structure based on the common element in each, then count the entries in the data structure and output the results to a file. Here is the code Kevin suggested to use:

Code:
my %Tickets = (); 
while(<FILE>){
   chomp; #if necessary
   # get you data into the list of scalars you posted
   # in your first post
   my @data = qw($numberprgn $assignment $uis_elapsed_start_time $close_time $res_anal_code $user_priority $subtype $uis_support_level  $uis_fstatus);
   for my $i (0..8) {
      $Tickets{$uis_tier}{$severity_code}{$type}[$i]+=$data[$i];
   }

# finished building records
# print records to file
open (FILE,">records.txt") or die "$!";
flock(FILE, 2);
foreach my $tiers (keys %Tickets) {
    foreach my $severity (keys %{ $Tickets{$tiers} }) {
      foreach my $type (keys %{ $Tickets{$tiers}->{$severity_code} }) {
         print FILE "$tiers,$severity,$type," . join(',',@{ $Tickets{$tiers}->{$severity}->{$type} }) . "\n";
      }
   }
}
close(FILE);

Here is the problem I am having. I can't figure out how to get the numbers I want into the @data array.

What I am doing is looping through an array called @problem like this:

Code:
	foreach $prob (@problem) {
		 chomp $prob;
		 my ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code, $user_priority,
		 $severity_code ) = split (/,/, $prob);
# Perfom a bunch of exclusions
		 
                 next unless ($logical_name ~= /xyz/);
                 next unless (etc. etc. etc.);

# Now I perform a buch of counts as in
           
                 if ($close_date < $rpt_date) {$count1++;)

and then once I have excluded all the tickets I don't want, and count up the tickets if they occured before a certain period or after a certain period etc. I need to get the counts for each somehow into @data for printing:

Code:
 $Tickets{$uis_tier}{$severity_code}{$type}[$i]+=$data[$i];

In other words, with my loop through the @problem array, I can get the counts for the TOTAL number of tickets for ALL Tier levels , severities and types, but what I need is individual counts for each:

Code:
$Tickets{$tiers}->{$severity}->{$type}

In other words:

Code:
                            CLOSED   OPENED    etc.

Tier1->Critical->Router        5        10     etc.

where the CLOSED 5 column will be the results of the ticket counts for that Tier level, criticality level and device type.

I hope that is not to confusing.

Ant help is appriciated.

Nick

Thanks,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Code:
foreach $prob (@problem) {
         chomp $prob;
         [COLOR=#ff0000]my @data[/color] = ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code, $user_priority, $severity_code ) = split (/,/, $prob);
# Perfom a bunch of exclusions
 
Kevin,

I'm not sure what your getting at. Was that an incomplete post?

Thanks,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Sorry to interfere, i just want to help

He told you to put the variables into a named array.
my @data=(.....
instead of having them like this
my(......

Is it a bit clearer now?
 
Let me expand on my brief example then:

Code:
foreach $prob (@problem) {
   chomp $prob;
   my ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code, $user_priority, $severity_code ) = split (/,/, $prob);
   # Perfom a bunch of exclusions
   next unless ($logical_name ~= /xyz/);
   next unless (etc. etc. etc.);
   [COLOR=#ff0000]my @data[/color] = ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code, $user_priority, $severity_code )
   # Now you can loop through the @data array
}

the @data array in this scenario is just a temporary variable that holds the specific values of @problems for each line of @problems that gets past all the exclusions. You can use the @data array to easily loop through each of the elements it stores to process your data however you need it processed:

Code:
for my $i (0..8) {
  $Tickets{$uis_tier}{$severity_code}{$type}[$i]+=$data[$i];
}

that seems easier than writing a seperate line for every variable you had previously split from the lines of @problem:

Code:
my ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code, $user_priority, $severity_code ) = split (/,/, $prob);

of course if you do this you have to keep the variables in the same order everytime to make sure the correct index of the anonymous array is incremented properly :

$Tickets{$uis_tier}{$severity_code}{$type}[$i]+=$data[$i];

but that should be no problem.







 
or something more like this could work:

Code:
foreach $prob (@problem) {
   chomp $prob;
   [COLOR=#ff0000]my @data[/color] = split (/,/, $prob);
   # Perfom a bunch of exclusions
   next unless ($data[0] =~ /xyz/);
   next unless ($data[1] eq 'foo');
   next unless ($data[2] > $bonk);
   next unless ($data[3] ne 'whatever')
   etc
   etc
   etc   
   # Now you can loop through the @data array
   for my $i (0..8) {
     $Tickets{$uis_tier}{$severity_code}{$type}[$i]+=$data[$i];
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top