将多维散列转换为csv

时间:2015-12-14 10:32:14

标签: perl multidimensional-array hashtable

我有一个多维哈希,我在屏幕上打印,我想将哈希值转换为CSV格式的数据。 我的脚本的示例输出

$VAR1 = {
          '1' => {
                   'EVENT_NAME' => '"x1"',
                   'NV_MANAGED_OBJECT_INSTANCE' => '"SubNetwork=ONRM_RootMo,SubNetwork=AXE,ManagedElement=WBSC1,BssFunction=BSS_ManagedFunction,BtsSiteMgr=WV5619"',
                   'EVENT_TYPE' => '"x5"',
                   'NV_MANAGED_OBJECT_CLASS' => '"BtsSiteMgr"',
                   'AdditionalText_Line03' => '"MO                                 RSITE           ALARM SLOGAN"',
                   'NV_SPECIFIC_PROBLEM' => '"RADIO X-CEIVER ADMINISTRATION MANAGED OBJECT FAULT"',
                   'ResynchEvent' => '"false"',
                   'SubNetwork' => '"ONRM_RootMo"',
                   'SubNetwork_0' => '"ONRM_RootMo"',
                   'NV_PROBABLE_CAUSE' => '"0"',
                   'AdditionalText_Line04' => '"RXOCF-87                           WV5619          BTS EXTERNAL"',
                   'RawCaptureTimeStamp' => '1449806147',
                   'BssFunction' => '"BSS_ManagedFunction"',
                   'DOMAIN_NAME' => '"ALARM_IRP_VERSION_1_1"',
                   'AdditionalText_Line02' => '"MANAGED OBJECT FAULT"',
                   'NV_PROPOSED_REPAIR_ACTIONS' => '""',
                   'NV_ACK_TIME' => '"12/11/15 4:55:45 AM"',
                   'AdditionalText_Line01' => '"RADIO X-CEIVER ADMINISTRATION"',
                   'NV_ACK_STATE' => '"2"',
                   'NV_SYSTEM_DN' => '"Ericsson OSS IRPAgent"',
                   'ManagedElement' => '"WBSC1"',
                   'SubNetwork_1' => '"AXE"',
                   'BtsSiteMgr' => '"WV5619"',
                   'NV_EVENT_TIME' => '"12/11/15 4:55:00 AM"',
                   'AdditionalText_Line06' => '"-ProbableCause(OSS)=Different causes possible for same message"',
                   'AdditionalText_LineCount' => '"7"',
                   'AdditionalText_Line05' => '"END"',
                   'AdditionalText_Line00' => '"*** ALARM 505 A3/APT \\"WBSC1/G14B/04/0\\"U 151211 0455"',
                   'NV_ADDITIONAL_TEXT' => '"*** ALARM 505 A3/APT \\"WBSC1/G14B/04/0\\"U 151211 0455\\nRADIO X-CEIVER ADMINISTRATION\\nMANAGED OBJECT FAULT\\n\\nMO                                 RSITE           ALARM SLOGAN\\nRXOCF-87                           WV5619          BTS EXTERNAL\\n\\nEND\\n-ProbableCause(OSS)=Different causes possible for same message"',
                   'NV_NOTIFICATION_ID' => '"2267705"',
                   'AdditionalText_Line07' => '"-ProbableCause(OSS)=Different causes possible for same message"',
                   'NV_ACK_USER_ID' => '""',
                   'NV_ALARM_ID' => '"55079020"',
                   'NV_PERCEIVED_SEVERITY' => '"4"'
                 },
          '0' => {
                   'EVENT_NAME' => '"x4"',
                   'NV_MANAGED_OBJECT_INSTANCE' => '"SubNetwork=ONRM_RootMo,SubNetwork=AXE,ManagedElement=WBSC1,BssFunction=BSS_ManagedFunction,BtsSiteMgr=WV5619"',
                   'EVENT_TYPE' => '"x5"',
                   'NV_MANAGED_OBJECT_CLASS' => '"BtsSiteMgr"',
                   'AdditionalText_Line03' => '"MO                                 RSITE           ALARM SLOGAN"',
                   'NV_SPECIFIC_PROBLEM' => '"RADIO X-CEIVER ADMINISTRATION MANAGED OBJECT FAULT"',
                   'ResynchEvent' => '"false"',
                   'SubNetwork' => '"ONRM_RootMo"',
                   'SubNetwork_0' => '"ONRM_RootMo"',
                   'NV_PROBABLE_CAUSE' => '"0"',
                   'AdditionalText_Line04' => '"RXOCF-87                           WV5619          BTS EXTERNAL"',
                   'RawCaptureTimeStamp' => '1449806146',
                   'BssFunction' => '"BSS_ManagedFunction"',
                   'DOMAIN_NAME' => '"ALARM_IRP_VERSION_1_1"',
                   'AdditionalText_Line02' => '"MANAGED OBJECT FAULT"',
                   'NV_PROPOSED_REPAIR_ACTIONS' => '""',
                   'NV_ACK_TIME' => '"12/11/15 4:55:45 AM"',
                   'AdditionalText_Line01' => '"RADIO X-CEIVER ADMINISTRATION"',
                   'NV_ACK_STATE' => '"2"',
                   'NV_SYSTEM_DN' => '"IRPAgent"',
                   'ManagedElement' => '"WBSC1"',
                   'SubNetwork_1' => '"AXE"',
                   ' Status_TicketType' => '0',
                   'BtsSiteMgr' => '"WV5619"',
                   'NV_EVENT_TIME' => '"12/11/15 4:55:00 AM"',
                   'AdditionalText_Line06' => '"-ProbableCause(OSS)=Different causes possible for same message"',
                   'AdditionalText_LineCount' => '"7"',
                   'AdditionalText_Line05' => '"END"',
                   'AdditionalText_Line00' => '"*** ALARM 504 A3/APT \\"WBSC1/G14B/04/0\\"U 151211 0451"',
                   'NV_ADDITIONAL_TEXT' => '"*** ALARM 504 A3/APT \\"WBSC1/G14B/04/0\\"U 151211 0451\\nRADIO X-CEIVER ADMINISTRATION\\nMANAGED OBJECT FAULT\\n\\nMO                                 RSITE           ALARM SLOGAN\\nRXOCF-87                           WV5619          BTS EXTERNAL\\n\\nEND\\n-ProbableCause(OSS)=Different causes possible for same message"',
                   'NV_NOTIFICATION_ID' => '"2267704"',
                   'NV_ACK_USER_ID' => '""',
                   'NV_ALARM_ID' => '"55079016"',
                   'NV_PERCEIVED_SEVERITY' => '"6"'
                 }
        };

负责将文件遍历到哈希的代码:

my $i=0;
open my $cap1, $file or die "Could not open $file: $!";
while( my $line = <$cap1>)  {
        my @pairs;
        my $name;
        my $value;
        if($line ne ''){
                @pairs = split / = /, $line;
                $name = $pairs[0];
                chomp($name);
                $value = $pairs[1];
                chomp($value);
        }
        if($name ne '' && $name !~ /^Status_/){
                $data{$i}{$name} = $value;
        }
        if($name =~ /^RawCaptureTimeStamp/){
                $i++;
        }
}
print Dumper \%data;

抬头:每组散列中的列都不固定。它是动态的。 但是,我在另一个数组中有所有可能的列名。因此,您可以假设我在数组文件中包含所有列(EVENT_NAME,EVENT_TYPE,...)。使用它我需要将现有的哈希转换为CSV。

1 个答案:

答案 0 :(得分:3)

自己找到解决方案:

foreach my $j (sort keys %data) {
        foreach (@uniqueNames) {
                if(exists($data{$j}{$_})){
                        print "$data{$j}{$_},";
                }
                else{
                        print "\"\"",;
                }
        }
        print "\n";
}

其中@uniqueNames是一个包含原始哈希表中所有可能唯一列的数组。