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

Discussion in 'Perl Misc' started by perl Newbie, May 5, 2009.

  1. perl Newbie

    perl Newbie Guest

    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";
    }
     
    perl Newbie, May 5, 2009
    #1
    1. Advertising

  2. perl Newbie

    J. Gleixner Guest

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

    perl Newbie wrote:
    > 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/;
    }

    > $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.
     
    J. Gleixner, May 5, 2009
    #2
    1. Advertising

  3. perl Newbie

    perl Newbie Guest

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

    On May 5, 11:25 pm, "J. Gleixner" <>
    wrote:
    > perl Newbie wrote:
    > > 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 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";
    }
     
    perl Newbie, May 6, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hannes Wyss

    [ANN] Spreadsheet::ParseExcel

    Hannes Wyss, Feb 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    238
    Anu Sebastian
    Aug 4, 2009
  2. Marko Faldix

    ppm: ppm install Spreadsheet::ParseExcel

    Marko Faldix, Jan 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    471
    James Willmore
    Jan 27, 2004
  3. goomania
    Replies:
    3
    Views:
    191
    bate_G
    Jun 2, 2006
  4. perl Newbie
    Replies:
    5
    Views:
    241
    jmcnamara
    May 6, 2009
  5. Jeff
    Replies:
    4
    Views:
    259
    C.DeRykus
    May 3, 2010
Loading...

Share This Page