jmcnamara / excel-writer-xlsx

Perl module to create Excel XLSX files.
https://metacpan.org/pod/distribution/Excel-Writer-XLSX/lib/Excel/Writer/XLSX.pm
Other
100 stars 51 forks source link

`write_row` does not recognize array slices #259

Closed hhg7 closed 3 years ago

hhg7 commented 3 years ago

When I try to use write_row to write an array, $worksheet -> write_row($row+1, 0, \@line[0..$max_co]);

I get an error like this: Not an array ref in call to write_row() at compare_sites.pl line 52.

I can work around this by declaring a temp array, like my @l = @line[0..$max_col]

but I think that you should know that array slices aren't recognized properly by write_row

jmcnamara commented 3 years ago

The write_row() method takes an array ref. The type of \@line is ARRAY but the type of \@[0..$max_col] is SCALAR. To write a slice you probably need [@line[0..$max_col]]. Here is an example:

#!/usr/bin/perl -w

use strict;
use warnings;

use Excel::Writer::XLSX;

my $workbook  = Excel::Writer::XLSX->new( 'test.xlsx' );
my $worksheet = $workbook->add_worksheet();

my @line = (0 .. 19);

print "1: ", ref \@line[0..5], "\n";
print "2: ", ref \@line, "\n";
print "3: ", ref [@line[0..5]], "\n";

$worksheet->write_row(0, 0, [@line[0..5]]);

$workbook->close();

__END__

Output:

1: SCALAR
2: ARRAY
3: ARRAY

screenshot