http://qs321.pair.com?node_id=132586

mikeB has asked for the wisdom of the Perl Monks concerning the following question:

The code below has two foreach loop headers. The one commented out works (i.e., loop on index numbers). The one not commented out does not work, but seems like it should. It runs through the loop the proper number of times, but the ->{Name} element is undefined.

Any thoughts?

#!/usr/bin/perl -w use strict; use Data::Dumper; use Spreadsheet::ParseExcel; my $excel = new Spreadsheet::ParseExcel or die; my $book = $excel->Parse($ARGV[0]) or die; my $sheet; foreach $sheet (@{$book->{Worksheet}}) { #foreach (0 .. $book->{SheetCount}) { # $sheet = $book->{Worksheet}[$_]; print $sheet->{Name},"\n"; }

Replies are listed 'Best First'.
Re: Problem enumerating Worksheets with Spreadsheet::ParseExcel
by jmcnamara (Monsignor) on Dec 17, 2001 at 22:34 UTC

    I get the opposite results. The uncommented loop works and the commented loop gives a warning.

    The warning occurs because SheetCount is the index of the last worksheets +1. So I guess this is what you want (although I prefer the other method):

    foreach (0 .. $book->{SheetCount} -1) { $sheet = $book->{Worksheet}[$_]; print $sheet->{Name},"\n"; }

    --
    John.