Hello,

I would like some help if anyone can. I have researched, research and I just cant do it anymore - I have to ask. Before I do, I know this is probably a simple answer, and your fed up of explainng it but from my research (lots) the example code seems to be a simple situation, and I have tried to undertake what has been said...I always assign a value to a variable. As you will see below I have but I still get the error (below) I have spent hours trial and error and I havnt even 'randomly' come upon the correct answer!

Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.
Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.
Use of uninitialized value $VarComparison
acro_Files/NO_DEL/AccessExcel.pl line 84.

My modified code:

#!/usr/bin/perl -w

# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);

# Right trim function to remove trailing whitespace
sub rtrim($)
{
	my $string = shift;
	$string =~ s/\s+$//;
	return $string;
}

use v5.12.1;
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::Warn = 3;                                # die on errors...

# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');  

# open Excel file
my $Book = $Excel->Workbooks->Open("C:/CH-53_Electronics/Macro_Files/NO_DEL/WDMASTER.xls"); 

# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface.  Excel's Visual Basic Editor has more
# information on the Excel OLE interface.  Here we just use the first
#20 worksheet, rows 1 through 4 and columns 1 through 3.

# select worksheet number 1 (you can also select a worksheet by name)
my $Sheet = $Book->Worksheets("GA");

unlink("C:/boo/boo1.txt");

my $record;
our $ansi;

my $VarFileName = 0;

my $VarISS = "VarISS_TestValue";
my $VarDWGDO = "VarDWGDO_TestValue";

#our $varComparison = 0;

############## READ FILE FROM ISODRAW ##################

open(INFILE, "<:encoding(UTF-16)", "C:/CH-53_Electronics/Macro_Files/Tmp_Iso_FileName.txt");
while(<INFILE>)
{
$record=$_;

print "$record \n";

$VarFileName = $record;

}
close(INFILE);

######## Trim the trailing whitespace ########
$VarFileName = rtrim($VarFileName);

#################################
# varDWGDO_Col is the column DWGDO, check that it is the 6th column in the excel.
my $VarDWGDO_Col = 6;

# VarISS_Col  is the column ISS, check that it is the 8th column in the excel.
my $VarISS_Col = 8;

# VarICN_Col  is the column ICN, check that it is the 16th column in the excel.
my $VarICN_Col = 16;

my $varFlag = 0;

foreach my $row (1..256)
{
	foreach my $col ($VarICN_Col)
	{
	my $VarComparison = $Sheet->Cells($row,$VarICN_Col)->{'Value'};
	if ($VarComparison eq $VarFileName)
		{
		$varFlag = 1;
		$VarISS = $Sheet->Cells($row,$VarISS_Col)->{'Value'}."\n";
		$VarDWGDO = $Sheet->Cells($row,$VarDWGDO_Col)->{'Value'}."\n";
		open WriteFILEISS, ">C:/boo/boo_ISS.txt" or die $!;
		print WriteFILEISS $VarISS;
		close WriteFILEISS;
		open WriteFILEDWGDO, ">C:/boo/boo_DWG.txt" or die $!;
		print WriteFILEDWGDO $VarDWGDO;
		close WriteFILEDWGDO;
		last;
		} else {
		       }
	}
	
}
if ($varFlag == 0)
	{
	open WriteFILEFAILED, ">>C:/boo/boo.txt1" or die $!;
	print WriteFILEFAILED $VarFileName."\n";
	close WriteFILEFAILED;
	open WriteFILEFAILEDERROR, ">C:/boo/boo.txt2" or die $!;
	print WriteFILEFAILEDERROR 1;
	close WriteFILEFAILEDERROR;
	} else {
	open WriteFILEFAILEDERROR, ">C:/boo/boo.txt3" or die $!;
	print WriteFILEFAILEDERROR 0;
	close WriteFILEFAILEDERROR;
	}

# clean up after ourselves
$Book->Close;

I have tried declaring and assigning $VarComparison next to my $VarICN_Col = 16;
and making it global(our) ands it just doesnt want to work. I have added in everything to assist with the line # but I think the issue is around the for each loops everywhere else seems fine. I had this working originally but needed to change a few things around, the main things I changed was ($VarICN_Col) which used to be ($VarDWGDO_Col) - thats all. I havnt touched $varComparison.


Can someone please help me out here and explain why it doesnt work.

Many thanks

Alan

Sorry, I probably can't help you because my platform is Linux and I don't have Excel, etc. so can't test your script. I really don't see anything wrong with line 84 of your script.

Can you recreate the error in a simpler script without all the presumably irrelevant (irrelevant to the "uninitialized value $VarComparison error") stuff?

Edited 5 Years Ago by d5e5: n/a

Have you tried looking to see what is in $VarComparison after this assignment?

my $VarComparison = $Sheet->Cells($row,$VarICN_Col)->{'Value'};

Just a thought.

Hi, ColMatrix,

I've looked in the CPAN documentation, and your code seems to be in order,
So, as mentioned above, it is most likely a matter of the field you assign to $VarComparison being empty, and therefore $VarComparison being empty as well. To verify that, add a

print "**$VarComparison**\n"

line before line 84 and see what does that variable include in each iteration. To get a better idea of what's happening in the loop, make the print more elaborate:

print "The value of cell $row,$VarICN_Col is '$VarComparison'\n"

This will help you in locating the cells at the sheet.

Finally, if this is indeed the issue you can change line 84 to say

if ($VarComparison && $VarComparison eq $VarFileName)

This will eliminate all the instances that the cell is empty from the checks.

A personal note, never be afraid to ask anything, at the least people here can direct you to where you can further your research.

Comments
Good suggestion.
This article has been dead for over six months. Start a new discussion instead.