Not Initializing Variables Correctly?

B

Buck Turgidson

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,SearchOrder=>
xlByRows})->{Row};

$LastCol = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,SearchOrder=>xlByColumns})->{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;
 
P

Paul Lalli

Buck Turgidson said:
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!!!!"

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
 
T

Tad McClellan

Bob Walton said:
That seems like reasonable behavior: if the
cell has no value, it generates undef in Perl.


I would say that representing an empty spreadsheet cell as
an empty string would be ever so much more reasonable. :)

I'd reserve undef for cells outside the 2-D limits of the spreadsheet.
 
N

nobull

Paul Lalli said:
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,
"Use of uninitialized value [...]"

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

This is true.
This leads me to believe I am [...] not using variables correctly.

The OP is misreading the warning incorrectly (sic).

The phase "uninitialized value" in not meaningful. Most people's
brain's subconciously attempt to make it meaningful by misreading it.
The OP has misread it as "uninitialized variable". The correct way to
misread it is "undefined value".
These days, it is preferred to add the line
use warnings;
in place of the -w switch

In particular the "use warnings" mecahanism allows you to fine tune
the warnings you want. In particular there are a number of contexts
where undefined values can safely be treated as strings and in that
case the best thing to do is simply suspect that warning for the
duration of the operation.
 
N

nobull

Tad McClellan said:
I would say that representing an empty spreadsheet cell as
an empty string would be ever so much more reasonable. :)

I'd reserve undef for cells outside the 2-D limits of the spreadsheet.

Now, I can see a smiley in there so I think you are joking. I _hope_
you are joking. On first reading I did not see the smiley there. I
fear that some others reading this may not realise it's a joke.

So just to be clear. Tad you were joking, weren't you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top