Help using Spreadsheet::ParseExcel Module - Can't call method "value"on an undefined value

P

perl Newbie

Hi,

Please help me understand why I am getting error { Can't call method
"value" on an undefined value excelRead.pl line 34. }

Line 34 in my script is $qval = $qcell->value();

When I checked the excel file, there are no blank rows in column 1,
then how come it is throwing an error -undefined value?

Thanks!


__CODE__

use strict;
use warnings;
use Spreadsheet::parseExcel;


my $excelfilename="Data2.xls";
my $datasheet="data";


my $parser = Spreadsheet::parseExcel->new();
my $workbook = $parser->Parse($excelfilename);
my $worksheet = $workbook->Worksheet($datasheet);
my ( $row_min, $row_max ) = $worksheet->row_range();
my $row;
my $cell;
my $cell_value;

my $qcell;
my $qval;
my @qids;

for $row ($row_min .. $row_max) {
$cell = $worksheet->get_cell($row,3);
$cell_value = $cell->value();

if ($cell_value =~/single choice/i) {
$qcell = $worksheet->get_cell($row+1,0);
$qval = $qcell->value();
until ($qval =~/question/) {
$qcell = $worksheet->get_cell($row
+1,0);
$qval = $qcell->value();
push @qids, $worksheet->get_cell($row
+1,1)->value();
$row++;
}
}
}

foreach my $l (@qids) {
print $l, "\n";
}
 
J

J. Gleixner

perl said:
Hi,

Please help me understand why I am getting error { Can't call method
"value" on an undefined value excelRead.pl line 34. }

Line 34 in my script is $qval = $qcell->value();

When I checked the excel file, there are no blank rows in column 1,
then how come it is throwing an error -undefined value? [...]
my $row;
my $cell;
my $cell_value;

my $qcell;
my $qval;
my @qids;

Define the variable in the smallest possible scope.
for $row ($row_min .. $row_max) {
for my $row ( $row_min .. $row_max) {
$cell = $worksheet->get_cell($row,3);
my $cell = $worksheet->get_cell($row,3);

etc.
$cell_value = $cell->value();

if ($cell_value =~/single choice/i) {
$qcell = $worksheet->get_cell($row+1,0);

What happens when $row equals $row_max.. e.g. what's in ($row_max+1, 0)?

You could use another for loop, which would make your code much cleaner.

for my $next_row ( $row+1 .. $row_max ) {
# get the values you're after..
last if $qval =~ /question/;
}

Be careful modifying a variable you use in your for loop. In
this case perl saves you, but you should avoid doing it.

FYI: You can add print statements to your code, to help you debug things
on your own.
 
P

perl Newbie

perl said:
Please help me understand why I am getting error { Can't call method
"value" on an undefined value excelRead.pl line 34. }
Line 34 in my script is $qval = $qcell->value();
When I checked the excel file, there are no blank rows in column 1,
then how come it is throwing an error -undefined value? [...]
        my $row;
        my $cell;
        my $cell_value;
        my $qcell;
        my $qval;
        my @qids;

Define the variable in the smallest possible scope.


        for $row ($row_min .. $row_max) {

for my $row ( $row_min .. $row_max) {>                 $cell = $worksheet->get_cell($row,3);

my $cell = $worksheet->get_cell($row,3);

etc.
                $cell_value = $cell->value();
                if ($cell_value =~/single choice/i) {
                        $qcell = $worksheet->get_cell($row+1,0);

What happens when $row equals $row_max.. e.g. what's in ($row_max+1, 0)?

You could use another for loop, which would make your code much cleaner.

                        for my $next_row ( $row+1.. $row_max ) {
                                # get thevalues you're after..
                                last if $qval =~ /question/;
                        }

 >                               $row++;

Be careful modifying a variable you use in your for loop. In
this case perl saves you, but you should avoid doing it.

FYI: You can add print statements to your code, to help you debug things
on your own.

You could use another for loop, which would make your code much cleaner.
for my $next_row ( $row+1 .. $row_max ) {
# get the values you're after..
last if $qval =~ /question/;
}

Thanks for the help, using another loop solved the problem.

Why I am getting error if I loop using until?

__CODE__
use strict;
use warnings;
use Spreadsheet::parseExcel;

my $excelfilename="data.xls";
my $datasheet="data";

my $parser = Spreadsheet::parseExcel->new();
my $workbook = $parser->Parse($excelfilename);
my $worksheet = $workbook->Worksheet($datasheet);
my ( $row_min, $row_max ) = $worksheet->row_range();
my $row;
my $rowL;
my @qids;

for $row ($row_min .. $row_max) {
if ($worksheet->get_cell($row,3)->value() =~/single choice/i)
{
##print "Row: $row ", $worksheet->get_cell($row,3)-
value(), "\n";
$rowL=$row+1;
for $rowL ($rowL .. $row_max ) {
last if ($worksheet->get_cell($rowL,0)->value()
=~/question/);
##until ($worksheet->get_cell($rowL,0)->value()=~/
question/) {
if ($worksheet->get_cell($rowL,0)->value()=~/
column/) {
##print "RowL: $rowL ", $worksheet-
get_cell($rowL,6)->value(), "\n";
push @qids, $worksheet->get_cell($rowL,
6)->value();
}
$rowL++;
}
}
next;
}

foreach my $l (@qids) {
print $l, "\n";
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top