如何填充o / p下面的散列哈希值: O / P: -
InterfaceName : Ethernet
Name : HP FlexFabric 10Gb 2-port 554FLB Adapter
MAC : 00:17:A4:77:00:28
IP : 169.254.39.166
PNPDeviceID : PCI\VEN_19A2&DEV_0710&SUBSYS_337B103C&REV_01\4&2012321E&0&0160
Manufacturer : Emulex
InterfaceName : Ethernet 2
Name : HP FlexFabric 10Gb 2-port 554FLB Adapter #2
MAC : 00:17:A4:77:00:26
IP : 10.10.2.122
PNPDeviceID : PCI\VEN_19A2&DEV_0710&SUBSYS_337B103C&REV_01\4&2012321E&0&0060
Manufacturer : Emulex
InterfaceName : Ethernet 5
Name : HP Flex-10 10Gb 2-port 530M Adapter #58
MAC : 00:17:A4:77:00:2A
IP : 10.10.2.121
PNPDeviceID : EBDRV\L2ND&PCI_168E14E4&SUBSYS_17A5103C&REV_10\5&390687C9&0&20050500
Manufacturer : Hewlett-Packard Company
InterfaceName : Ethernet 6
Name : HP Flex-10 10Gb 2-port 530M Adapter #59
MAC : 00:17:A4:77:00:2C
IP : 169.254.92.18
PNPDeviceID : EBDRV\L2ND&PCI_168E14E4&SUBSYS_17A5103C&REV_10\5&2CC241CC&0&20050500
Manufacturer : Hewlett-Packard Company
InterfaceName : Ethernet 9
Name : HP FlexFabric 10Gb 2-port 534M Adapter #61
MAC : AC:16:2D:7D:CA:FC
IP : 169.254.229.98
PNPDeviceID : EBDRV\L2ND&PCI_168E14E4&SUBSYS_1933103C&REV_10\5&31405534&0&20050800
Manufacturer : Hewlett-Packard Company
InterfaceName : Ethernet 10
Name : HP FlexFabric 10Gb 2-port 534M Adapter #62
MAC : AC:16:2D:7D:CA:F8
IP : 169.254.203.212
PNPDeviceID : EBDRV\L2ND&PCI_168E14E4&SUBSYS_1933103C&REV_10\5&E3EB2D7&0&20050800
Manufacturer : Hewlett-Packard Company**
哈希哈希我需要的是以下格式。
%hash = (Ethernet => {
Name => “HP Flex-10 10Gb 2-port 530M Adapter #58”,
MAC => “00:17:A4:77:00:28”,
IP => “ 169.254.39.166”,
PNPDeviceID => “PCI\VEN_19A2&DEV_0710&SUBSYS_337B103C&REV_01\4&2012321E&0&0160”
Manufacturer => “Emulex”},
与以太网1,2等相似。 );
答案 0 :(得分:1)
又快又脏:
use warnings;
use strict;
local $/ = 'InterfaceName : ';
my %master;
while (<DATA>) {
my @fields = split(/\n/);
my $ifname = shift @fields;
pop(@fields) if defined $fields[-1] and $fields[-1] =~ /^\s+/;
for my $field (@fields) {
my ($key, $value) = split(/:/, $field, 2);
$master{$ifname}{$key} = $value;
}
}
use Data::Dumper;
print Dumper(\%master);
__DATA__
InterfaceName : Ethernet
Name : HP FlexFabric 10Gb 2-port 554FLB Adapter
等等,等等......等等
现在,有什么问题吗?
要了解$/
的作用,请参阅man perlvar
。
要了解split
的作用,请参阅perldoc -f split
。
要了解shift
的作用,请参阅perldoc -f shift
。
$master{$ifname}{$key}
是 autovivification (Google it)的一个例子
答案 1 :(得分:1)
my $s = do { local $/; <> };
my %hash = map {
my (undef, $i, %h) = split /\s+:\s+|\n/;
$i ? ($i => \%h) : ();
}
split /InterfaceName/, $s;
use Data::Dumper; print Dumper \%hash;
输出
$VAR1 = {
'Ethernet' => {
'PNPDeviceID' => 'PCI\\VEN_19A2&DEV_0710&SUBSYS_337B103C&REV_01\\4&2012321E&0&0160 ',
'IP' => '169.254.39.166',
'Manufacturer' => 'Emulex ',
'MAC' => '00:17:A4:77:00:28 ',
'Name' => 'HP FlexFabric 10Gb 2-port 554FLB Adapter '
},
...