> Programming Languages > Perl
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Perl



Perl - "Not Initializing Variables Correctly?" in Programming Languages


Old 11-18-2004   #1
.... ..rgids..
 
Default Not Initializing Variables Correctly?

I am a very weak Perl programmer, but I have managed to put the following
together, based on some examples I've found on the web.

However, I receive a long series of the following error message, but the
problem is it is not consistent as to where it begins and ends (i.e. which
cell in the Excel spreadsheet). This leads me to believe I am overstepping
some memory, or not using variables correctly. I do not think there is
anything wrong with the spreadsheet.

I would be grateful if someone could tell me the error of my ways. I don't
even expect politeness.

"Use of uninitialized value in printf at h:\dev\perl\readbudget.pl line 29"


#!/usr/bin/perl -w

#use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

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

my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->Open("d:\\misc\\bud.xls");
my $Sheet = $Book->Worksheets('bud_05');

$LastRow =
$Sheet->UsedRange->Find({What=>"*",SearchDirection=>xlPrevious,Searc hOrder=>
xlByRows})->{Row};

$LastCol = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,SearchOrder=>xlByColum ns})->{Column};

foreach my $row (2..$LastRow)
{
my $descr1 = $Sheet->Cells($row,1)->{'Value'};
my $descr2 = $Sheet->Cells($row,2)->{'Value'};
my $descr3 = $Sheet->Cells($row,3)->{'Value'};
my $descr4 = $Sheet->Cells($row,4)->{'Value'};

foreach my $col (5..$LastCol)
{
my $budval = $Sheet->Cells($row,$col)->{'Value'};
printf
"%d,%d,%s,%s,%s,%s,%d\n",$row,$col,$descr1,$descr2 ,$descr3,$descr4,$budval;
}
}

$Book->Close;


 
Old 11-18-2004   #2
.... ..l..
 
Default Re: Not Initializing Variables Correctly?

"Buck Turgidson" <jc_va@hotmail.com> wrote in message
news:831v62-i05.ln1@turf.turgidson.com...
> I am a very weak Perl programmer,


Then it would make sense to ask the interpreter for all the help you can
get, no?

> but I have managed to put the following
> together, based on some examples I've found on the web.
>
> However, I receive a long series of the following error message,


Those are not error messages, they are warnings. There is a rather
significant difference.

> but the
> problem is it is not consistent as to where it begins and ends (i.e.

which
> cell in the Excel spreadsheet). This leads me to believe I am

overstepping
> some memory, or not using variables correctly. I do not think there

is
> anything wrong with the spreadsheet.
>
> I would be grateful if someone could tell me the error of my ways. I

don't
> even expect politeness.
>
> "Use of uninitialized value in printf at h:\dev\perl\readbudget.pl

line 29"
>
>
> #!/usr/bin/perl -w


These days, it is preferred to add the line
use warnings;
in place of the -w switch

> #use strict;


Now why on earth would you comment that out? That is an extremely
helpful line. If using that line gives you errors, the correct solution
is to *fix* those errors, not to ignore them. This is roughly the
equivalent of sticking your fingers in your ears and shouting "LA LA LA
I CAN'T HEAR YOU!!!!"

<snip a bunch of code>

> foreach my $row (2..$LastRow)
> {
> my $descr1 = $Sheet->Cells($row,1)->{'Value'};
> my $descr2 = $Sheet->Cells($row,2)->{'Value'};
> my $descr3 = $Sheet->Cells($row,3)->{'Value'};
> my $descr4 = $Sheet->Cells($row,4)->{'Value'};


Are you sure you don't want maybe an array or a hash here? In general,
a sequence of similarly named scalar variables indicates a poorly
thought out data structure.

> foreach my $col (5..$LastCol)
> {
> my $budval = $Sheet->Cells($row,$col)->{'Value'};
> printf
>

"%d,%d,%s,%s,%s,%s,%d\n",$row,$col,$descr1,$descr2 ,$descr3,$descr4,$budv
al;

I would say the first step in debugging your problem would be to figure
out *which* variable is not defined. A quick and dirty way would be to
seperate each of the printf's...

print "$row,";
print "$col,";
#...
print "$budval\n";

And then determine based on the warning's line number which variable is
undefined. (Once you've fixed the problem, of course, you should revert
to something neater looking...)
print join ',', $row,$col,@descr,$budval;
print "\n";


Hope that at least points you in the right direction.
Paul Lalli

 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0