0

Below is the perl code

use Spreadsheet::WriteExcel;

my $workbook = new Spreadsheet::WriteExcel( 'one_worksheet.csv' );

my ${path_1} = $workbook->add_worksheet('Max Input');
my ${path_2} = $workbook->add_worksheet('Min Input');

foreach (keys %max_hash ) {
    ${$_}->write(0, $column, "$max_hash{$_}" );
    $column = $column + 1;
}

I get this error

"Can't use string ("path_1") as a SCALAR ref while "strict refs" in use".

%max_hash has the keys path_1 and path_2.

What is the solution?

Borodin
  • 126,100
  • 9
  • 70
  • 144
Bharath
  • 11
  • 2
  • Could you try to [format the code](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)? – Martin Gottweis Nov 24 '16 at 10:07
  • Personal suggestion, perhaps have a look at `Excel::Writer::XLSX` if that would fit your requirements, it has really nice documentation as well. Im not implying that would be a solution, I just saw it as a simpler excel writer :) – Recct Nov 24 '16 at 12:34

2 Answers2

3

The syntax my ${path_1} is unusual, but not actually a problem. With use warnings turned on, you'll get a warning message:

Ambiguous use of ${path_1} resolved to $path_1

Best to just replace it with my $path_1.

Your actual problem is this line:

${$_}->write(0,$column,"$max_hash{$_}");

You're trying to use something called "symbolic references" (also know as "using a variable as the name of a variable"). And they are a very bad idea for all sorts of reasons - which is why use strict prevents you from doing it.

Far better to store your worksheets in a hash.

use Spreadsheet::WriteExcel;

# "Class->new()" is far better than "new Class"
my $workbook = Spreadsheet::WriteExcel->( 'one_worksheet.csv' );
my %worksheets;
$worksheets{path_1} = $workbook->add_worksheet('Max Input');
$worksheets{path_2} = $workbook->add_worksheet('Min Input');
foreach(keys %max_hash) {
    $worksheets{$_}->write(0, $column, $max_hash{$_});
    $column = $column + 1;
}
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

You don't use curly braces when declaring variables. Use:

my $path_1 = $workbook->add_worksheet('Max Input');

${something} syntax means dereference, so perl treats something as a reference. Besides, it's wrong to use it with my.