简体   繁体   English

Perl循环输出到Excel电子表格

[英]Perl Loop Output to Excel Spreadsheet

I have a Perl script, the relevant bits of which are posted below. 我有一个Perl脚本,其相关内容如下。

# Pull values from cells
        ROW:
        for my $row ( $row_min + 1 .. $row_max ) {
            my $target_cell   = $worksheet->get_cell( $row, $target_col);
            my $response_cell = $worksheet->get_cell( $row, $response_col);

            if ( defined $target_cell && defined $response_cell ) {
                my $target   = $target_cell->value();
                my $response = $response_cell->value();

# Determine relatedness
                my $value      = $lesk->getRelatedness($target, $response);

# Copy output to new Excel spreadhseet, 'data.xls'
                my $workbook1  = Spreadsheet::WriteExcel->new('data.xls'); 
                my $worksheet1 = $workbook1->add_worksheet();
                $worksheet1->set_column(0, 3, 18);
                my $row = 0;

                foreach ($target) {
                $row++;
                $worksheet1->write( $row, 0, "Target      = $target\n");
                $worksheet1->write( $row, 1, "Response    = $response\n");
                $worksheet1->write( $row, 2, "Relatedness = $value\n");
                }
            }
        }

This script uses the Perl modules ParseExcel and WriteExcel. 该脚本使用Perl模块ParseExcel和WriteExcel。 The input data spreadsheet is a list of words under two columns, one labelled 'Target' and the other labelled 'Response.' 输入数据电子表格是两列下的单词列表,一列标记为“目标”,另一列标记为“响应”。 The script takes each target word and each response word and computes a value of relatedness between them (that's what the 脚本会提取每个目标词和每个响应词,并计算它们之间的相关性值(即

$lesk->getRelatedness

section of code is doing. 代码段正在执行。 It is calling a perl module called WordNet::Similarity that computes a measure of relatedness between words). 它正在调用一个称为WordNet :: Similarity的perl模块,该模块计算单词之间的相关性。

All of this works perfectly fine. 所有这些工作都很好。 The problem is I am trying to write the output (the measure of similarity, or $value in this script) into a new Excel file. 问题是我正在尝试将输出(此脚本中的相似度或$ value)写入新的Excel文件中。 No matter what I do with the code, the only output it will give me is the relatedness between the LAST target and response words. 无论我对代码做什么,它唯一能给我的输出就是LAST目标词与响应词之间的相关性。 It ignores all of the rest. 它忽略所有其余部分。

However, this only occurs when I am trying to write to an Excel file. 但是,这仅在我尝试写入Excel文件时发生。 If I use the 'print' function instead, I can see all of the outputs in the command window. 如果我改用“打印”功能,则可以在命令窗口中看到所有输出。 I can always just copy and paste this into Excel, but it would be much easier if I could automate this. 我总是可以仅将其复制并粘贴到Excel中,但是如果我可以自动化它会容易得多。 Any idea what the problem is? 知道是什么问题吗?

您每次将$ row的值重置为0。

Problem is solved. 问题解决了。 I just needed to move the 我只需要移动

my $workbook1 = Spreadsheet::WriteExcel->new('data.xls'); 
my $worksheet1 = $workbook1->add_worksheet();

lines to another part of the script. 行到脚本的另一部分。 Since they were in the 'for' statement, the program kept overwriting the 'data.xls' file every time it ran through the loop. 由于它们位于“ for”语句中,因此程序每次通过循环时都会覆盖“ data.xls”文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM