0

我正在编写一个解析 Excel 文件的 Perl 脚本。此脚本的目的是计算第 1 列中的每个单元格值,即第 2 列中的值的数量。

例如,一个看起来像这样的 Excel 文件:

12    abc
12    abc
12    efg
12    efg
13    hij
13    hij
13    klm

我的脚本将返回:

对于单元格值 12,我有:

2 values "abc", 2 values "efg" and for cell value 13 i have : 2 values "hij" and 1 value "klm". 

我的脚本看起来像这样(我从 perl 文档中获取了这个例子):

 use Spreadsheet::XLSX;

 my $excel = Spreadsheet::XLSX -> new ('Book1.xlsx');

 foreach my $sheet (@{$excel -> {Worksheet}}) {

    printf("Sheet: %s\n", $sheet->{Name});

    $sheet -> {MaxRow} ||= $sheet -> {MinRow}; 

     foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {

            $sheet -> {MaxCol} ||= $sheet -> {MinCol};

            foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {

                    my $cell = $sheet -> {Cells} [$row] [$col];

                    if ($cell) {
                        #here I should count the cell values 
                    }
                print $cell;
            }

    }


 }

我不知道如何做到这一点,因为我以前从未使用过 perl,而且我无法在网上找到与我想要的完全匹配的示例。任何帮助将不胜感激。谢谢

4

2 回答 2

0

使用哈希。用 计数$hash{$column1}{$column2}++。遍历键并打印计数值。是的,我给你留下了一些工作来填充 column1、column2 的值,并迭代哈希。

于 2012-11-14T20:07:29.743 回答
0

也许以下注释脚本会有所帮助:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;

# No need to iterate through columns, so set val for col 1
my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('Book1.xlsx');

# Just get the first sheet
my $sheet = ${ $excel->{Worksheet} }[0];

# Calculate the range of rows
$sheet->{MaxRow} ||= $sheet->{MinRow};

# Iterate through each row
foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {

    # The cell in column 1
    my $cell = $sheet->{Cells}[$row][$col1];

    if ($cell) {

        # The adjacent cell in column 2
        my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];

        # Use a hash of hashes
        $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;
    }
}

# Numerically sort the keys; the value is a hash reference
for my $key1 ( sort { $a <=> $b } keys %hash ) {
    print "For cell value $key1: ";

    # Dereference the hash reference and get the keys/values
    while ( my ( $key2, $val2 ) = each %{ $hash{$key1} } ) {
        print qq{$val2 value(s) "$key2" };
    }
    print "\n";
}

# Show the hash structure
print "\n", Dumper \%hash;

输出:

For cell value 12: 2 value(s) "abc" 2 value(s) "efg" 
For cell value 13: 1 value(s) "klm" 2 value(s) "hij" 

$VAR1 = {
          '13' => {
                    'klm' => 1,
                    'hij' => 2
                  },
          '12' => {
                    'abc' => 2,
                    'efg' => 2
                  }
        };

您可以执行以下操作来显示与键 '13' 关联的值:

# Show only the value(s) for key '13'
print "For cell value 13: ";

# Dereference the hash reference for key '13' and get the keys/values
while ( my ( $key2, $val2 ) = each %{ $hash{13} } ) {
    print qq{$val2 value(s) "$key2" };
}

输出:

For cell value 13: 1 value(s) "klm" 2 value(s) "hij"
于 2012-11-14T22:49:45.800 回答