-3

如何填充以下 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 等一样。);

4

2 回答 2

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  '
                        },
...
于 2013-10-15T14:02:49.327 回答
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}是自动生存的一个例子谷歌它)

于 2013-10-15T13:55:08.117 回答