简体   繁体   English

散列值的Perl正则表达式

[英]Perl Regular Expression for Hash values

I have hash values which looks like this: 我有看起来像这样的哈希值:

   build -var ver=2.1.0.0 -var buildnum=WorkSet/2.1.0_suiteB_3o -var product=cisco6500 platform=NSA prodtype=Debug HOST=10.9.33.22 -var clist=

   build -var ver=2.1.0.0 -var buildnum=WorkSet/2.1.0_suiteB_3o -var product=juniper6700 platform=NSA prodtype=Prod HOST=10.9.33.22 -var clist=

I am trying to seperate out the hash value to create another hash such that it looks as below: 我试图分离出哈希值以创建另一个哈希,如下所示:

   My $list = {'2.1.0.0' => { 'branch' => Workset/2.1.0'
               'products' => [{product => 'cisco6500',
                                prodType => 'Debug'},
                                {product => 'juniper6700',
                                prodType => 'Prod'}]

} } }}

I have written something like this: 我写了这样的东西:

    if ($cmd =~ m/ver=(\d.\d.\d.\d)/){
            $version = $1;
    }
    if ($cmd =~ m/buildnum=(\w+.\/\d.\d.\d)/){
            $branch = $1;
    }
    if ($cmd =~ m/product=(\s+)/){
            $product = $1;
    }

} }

Now, I getting confused how to insert the hash in the hash and array simultaneously. 现在,我很困惑如何在哈希和数组中同时插入哈希。 Please if anyone can suggest something. 如果有人可以提出建议,请。 Thank you. 谢谢。

The branch can be built without a problem: 可以毫无问题地建立分支:

$list->{$version}{branch} = $branch;

For products, you should push them onto the array that you have to dereference: 对于产品,应将它们推到必须取消引用的阵列上:

push @{ $list->{$version}{products} },
        { product  => $product,
          prodType => $prodtype,
        };

In recent versions of Perl, the dereference is not needed, so you can push directly to an array reference. 在最新版本的Perl中,不需要取消引用,因此您可以直接推送到数组引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM