Advertisement

05.06.2008 at 04:14PM PDT, ID: 23381227
[x]
Attachment Details

creating a dynamic 2 dimensional array in perl

Asked by jetbet in Perl Programming Language

Tags: perl

I am attempting to scan through large log files and get a count of the various error responses.

To do this I need to see if the current response is already in my 2D array (as the first element) and return either the index or -1. I have created a sub called GetArrayIndex but I do not seem to be able to access the first element.
Can someone please tell me the basic mistake/s that I have made.

Thanks


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
my @response_array = ([0,0]);
my $response_code;
my $index;
my $array_length = 1;
my @log_files = glob('input/*.xml');
  foreach my $file (@log_files)
  {
       print("  processing $file \n");
       open(INPUT, $file);
       while (<INPUT>)
       {
          $line = $_;
          if ($line =~ /.*<Response_Code>(.*)<\/Response_Code>.*/)
          {
             $response_code = $1;
             $index = GetArrayIndex($response_code, @response_array);
             if ($index == -1)
             {
             	$array_length = push(@response_array, ([$response_code, 1]));
             	print("new $array_length $index $response_code \n");
             }
             else
             {
              $response_array[$index][1] = $response_array[$index][1] + 1;
              if ($response_code != 0)
              {
              print("$response_array[$index][0] : $response_array[$index][1] \n");
              }
 
             }
 
          }
      }
  }
      
###############################################################################
sub GetArrayIndex {
 
  my ($input, @error_array) = @_;
  my $place = 0;
  my $row;
  my $error;
  my $count;
  my $result = -1;
  my $size = length(@error_array);
  for (my $i = 0; $i < $size; $i++)
  {
  	print("$size \n");
  	$row = @error_array[$i];
    if ($row[0] == $input)
    {
      print("$row[1] : $input \n");
      $result = $place;
      last;
    }
    $place++
  }
  
return $result;
}
 
###############################################################################
[+][-]05.06.2008 at 04:29PM PDT, ID: 21511829

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: perl
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.06.2008 at 04:39PM PDT, ID: 21511874

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628