Perl:将数据从哈希转储到excel

时间:2016-04-13 14:31:02

标签: arrays excel perl hash

我有一个带键和值(数组)的哈希。我想将它们转储到电子表格中,但很难安排它们。

%哈希
key1 - > foo bar
key2-> john adam gill
key3->苹果香蕉芒果橙

代码:

use strict;
use warnings;
use Excel::Writer::XLSX;

my $pattern = "BEGIN_";
my $format;
my @keys = qw(key1 key2 key3);
foreach my $key(@keys){
     open my $fh, "<","filename.txt" or die $!;
      while ( <$fh> ) {
        if (/$pattern/) {
        push(@matching_lines, $_);
      }
    }
    $hash{$key} = [@matching_lines] ; 
   for (@matching_lines) { $_ = undef } ; #Emptying the array contents,to reuse it for for all the other keys
}

my $workbook = Excel::Writer::XLSX->new( 'c:\TEMP\filename.xlsx' ); 
 if (not defined $workbook)
{
    die "Failed to create spreadsheet: $!";
}
my $worksheet = $workbook->add_worksheet();

#  Add and define a format
$format = $workbook->add_format();
$format->set_bg_color( 'yellow' );

my $row = 1;
my $col = 0;

foreach my $k (keys %hash)
{
     $worksheet->write($row, $col, $k, $format); # title
     $worksheet->write_col($row+1, $col, $hash{$k}); #value
     $col++;
}
$workbook->close() or die "Error closing file: $!";   

当前输出

enter image description here
期望输出

enter image description here

2 个答案:

答案 0 :(得分:4)

编辑:现在您实际上已经更新了您的计划,以澄清问题是您如何阅读您的数据,以下是没有实际意义的。但它确实说明了另一种方法。

好的,这里的核心问题是你要做的就是翻转&#39;哈希。您逐行打印,但您的哈希按列组织。

使用逗号sep作为打印实际Excel的快速代理:

#!/usr/bin/env perl
use strict;
use warnings;

use Data::Dumper;

#initialise
my %hash = (
    key1 => [qw (  foo bar  )],
    key2 => [qw (  john adam gill  )],
    key3 => [qw (  apple banana mango orange )],
);

#print for debug
print Dumper \%hash;


#get header row. Sort it, because hashes are unordered.
#could instead:
#my @keys = qw ( key1 key2 key3 );
my @keys = sort keys %hash;
#print header row
print join ",", @keys, "\n";

#iterate until every element of the hash is gone
while ( map { @{ $hash{$_} } } @keys ) {
    #cycle the keys, shifting a value of the top of each array. 
    #replace any undefined values with ''. 
    print shift( @{ $hash{$_} } ) // '', "," for @keys;
    print "\n";
}

打印:

key1,key2,key3,
foo,john,apple,
bar,adam,banana,
,gill,mango,
,,orange,

如果将其作为csv加载到Excel中,则应该提供所需的结果。我很确定你可以使用类似的“写行”#39;与模块。

所以这实际上似乎做你想要的:

#!/usr/env/perl
use strict;
use warnings;

use Excel::Writer::XLSX;

#initialise
my %hash = (
    key1 => [qw (  foo bar  )],
    key2 => [qw (  john adam gill  )],
    key3 => [qw (  apple banana mango orange )],
);


my $workbook = Excel::Writer::XLSX->new('c:\TEMP\filename.xlsx');
if ( not defined $workbook ) {
    die "Failed to create spreadsheet: $!";
}
my $worksheet = $workbook->add_worksheet();

#  Add and define a format
my $format = $workbook->add_format();
$format->set_bg_color('yellow');

my @keys = sort keys %hash;
my $row  = 0;
$worksheet->write_row( $row++, 0, \@keys, $format );
while ( map { @{ $hash{$_} } } @keys ) {
    my $col = 0;
    $worksheet->write( $row, $col++, shift( @{ $hash{$_} } ) // '' )
        for @keys;
    $row++;
}
$workbook->close() or die "Error closing file: $!";

Output

答案 1 :(得分:2)

您没有正确清空@matching_lines数组。这一行:

for (@matching_lines) { $_ = undef } 

将数组值设置为undef,但不会删除它们 例如,如果@matching_lines('foo', 'bar'),则现在变为(undef, undef)。稍后向其添加bazqux时,它会变为(undef, undef, 'baz', 'qux')。将这些undef添加到工作表时,这些@matching_lines = (); 将成为空白单元格。

要正确清空阵列,请使用:

(function() {
    var a = 3.1415926, b = 2.718;
    var i, j, d1, d2;
    for(j=0; j<10; j++) {
        for(i=0; i<100000000; i++) {
            a = a + b;
        }
    }
    console.log("a = " + a);
})();