perverted contexts

gav on 2002-07-18T17:22:30

I had problems with reading XLS files with Spreadsheet::ParseExcel and ending up with a last row which was all undefs. I came up with:

next if @row == grep !$_, @row;
I think that looks quire nice :)


truth

wickline on 2002-07-18T19:49:15

>     next if @row == grep !$_, @row;

I'm not familiar with that module, but unless it returns references,
I can imagine zero being a valid value in a final row. If zero is
represented by [0] instead of 0 then maybe that's a non issue.

It sounds like you're expecting an empty string for an empty cell.
In that case, you could check length or defined instead of truth.

-matt

Re:truth

gav on 2002-07-18T22:23:38

grep in scalar context returns the number of items in the array that matched. I'm trying to make sure that there is at least 1 non-false value in the row. A row of all zeros would cause it to be skipped, but then that's bad data anyway :)

Probably this would have been more clear:

next unless grep $_, @row;
The module doesn't return rows in any nice way, it's a fantastically useful module but the interface seems too much like Visual Basic for my liking:
foreach my $r ($sheet->{MinRow}+1 .. $sheet->{MaxRow}) {
    my @row = map { $_ ? $_->Value : undef } @{$sheet->{Cells}[$r]}[0..$cols];
    push @items, \@row;
}

On my to-do list is write a wrapper, Spreadsheet::ParseExcel::Simple or somesuch.

Re:truth

jdavidb on 2002-07-19T15:35:16

I like the DBD::Excel wrapper, myself, but it may not suit your purposes.