CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Array Element Comparison Error - Uninitialized value of string eq error

 



spiitfire
Novice

Apr 15, 2006, 8:23 AM

Post #1 of 4 (2850 views)
Array Element Comparison Error - Uninitialized value of string eq error Can't Post

Hello All,

I was hoping I wouldn't have any more problems with my code but have stumbled on an error that i have spent a few hours trying to solve.

Use of Uninitialized value of string eq error at firewall.pl line 104, <FILE> line***(all the lines it reads in).

Testing each section individualy asides from the check seems to show promise, although the code definately could be cleaned up and made more efficient but ill get to that once it is running.

The error I can see seems to be when comparing the array elements values against each other. I'm using 'eq' to check this and would assume that this would be correct to use, but am generating the error for every single line scanned in during run time.

This sub is reading in a file and processing this into an array line by line, then tranfering the appropriate fields into another array. Checking the fields to see if they have been seen before, if so increasing the count for this otherwise creating a new element line. This is to cycle over the whole file untill completed then results outputted to the screen.



Code
sub processlog { 

my @log;
my @servicereport;
my @temp;
my $line;
my $count = 0;
my $secondcount=1;
my $second=0;
my $i = 0;
open (FILE, "fwlog") or
die "\nError unable to open fwlog!";

while(<FILE>)
{
next unless /;log;/ ;
chomp;
$line = <FILE>;
@temp = split(/;+/, $line);
unless ($line =~ /EDT/)
{ #strange, needed as include non ;log; files:|
$log[0] = $temp[8];
$log[1] = $temp[11];
$log[2] = "1";
$count++;
}

for($i=0;$i<=$secondcount;$i++)
{
if($log[0] eq $servicereport[$i][0])
{
if($log[1] eq $servicereport[$i][1])
{
$servicereport[$i][2]++;
}
}
else
{
push @servicereport, [ @log];
$secondcount++;
}
}
}
for($i=0;$i<$secondcount;$i++)
{
$servicereport[$i][3] = (($servicereport[$i][2] / $count)*100);
$servicereport[$i][3] = sprintf ("%.2f", $servicereport[$i][3]);
}
print "\nService Usage: Top number of total\n";
print "================================================\n";

for($i=0;$i<$secondcount;$i++)
{
print "$servicereport[0][0]($servicereport[0][1])";
print "\t\t\t$servicereport[0][2] $servicereport[0][3]%\n";
}


}


Any hints into the right direction would be greatly appreciated on this as I am absolutely stumped.Blush

Regards,

Spiitfire


KevinR
Veteran


Apr 15, 2006, 11:19 AM

Post #2 of 4 (2846 views)
Re: [spiitfire] Array Element Comparison Error - Uninitialized value of string eq error [In reply to] Can't Post

A couple of things jump out at me. First the use of <FILE> twice could be screwing things up. Each time <FILE> is called it reads a line of the input file.


Code
        while(<FILE>)  
{
next unless /;log;/ ;
chomp;
$line = <FILE>;


so first you have assigned <FILE> to $_ by default then you assign the next line of the input file to $line;

if that's not the behavior you want change the above block to:


Code
	while($line = <FILE>)  
{
next unless $line =~ /;log;/;
chomp($line);
@temp = split(/;+/, $line);


The next thing that I see is you have declared the varaibles at the beginning of the block with "my" which is OK, like:

my @servicereport;

so now @serviceport is essentially an empty array ready for data to be assigned to it. But shortly after that you are using it to compare against:


Code
if($log[0] eq $servicereport[$i][0])


but as far as I can see $serviceport[$1][0] has never been assigned a value, it's an uninitialized value. If you had previoulsy populated @serviceport with data you need to import that data to the sub routine or define it within the sub routine (which you do later) before trying to do something with it. Maybe you can initialize @serviceport with some dummy data just to get things roliing:


Code
my @servicereport = ([0,0,0]);


but I am not sure if that will mess up the comparisons you are doing later. All that looping makes me think you should be using hashes instead of arrays. Build up a hash from the input file and use the hash to do the comparisons. Are you comparing all the lines of the input file against each other?
-------------------------------------------------


spiitfire
Novice

Apr 15, 2006, 11:23 PM

Post #3 of 4 (2838 views)
Re: [KevinR] Array Element Comparison Error - Uninitialized value of string eq error [In reply to] Can't Post

thank you for the reply, ill look into using hashes instead as well as changing the use of <FILE>.

As for the question you asked, the sub routine is suposed to read in a line, compare this to what is in @servicereport(Nothing untill the first run through). If the data is not found, then it will be pushed into the array, if it is then a counter $servicereport[$i][2] should increase in value. The data is not needed after the sub routine is finnished, as it is printed to the screen.

Back from work now and back to coding to hopefully solve it.

Thanks for the help.

Spiitfire


spiitfire
Novice

Apr 16, 2006, 1:04 AM

Post #4 of 4 (2836 views)
Re: [KevinR] Array Element Comparison Error - Uninitialized value of string eq error [In reply to] Can't Post

I've decided to switch over to use hashes now, as on the whole it looks like it will be easier.


(This post was edited by spiitfire on Apr 16, 2006, 5:53 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives