Perl生成复杂记录的哈希

时间:2018-02-20 19:48:06

标签: arrays perl output

我似乎无法弄清楚的大问题。 我需要输入这个复杂的哈希(数组?):

my $raterequest =  
{
    Shipment =>
    {
        Shipper =>
        {
            (static data here)
        },
        ShipTo =>
        {
            (static data here too)
        },
        Package =>
        [
            {
                PackagingType =>
                {
                    Code => '02',
                    Description => 'Package'
                },
                PackageWeight =>
                {
                    UnitOfMeasurement =>
                    {
                        Code => 'LBS'
                    },
                    Weight => $boxWt
                },
            },

            {
                PackagingType =>
                {
                    Code => '02',
                    Description => 'Package'
                },
                PackageWeight =>
                {
                    UnitOfMeasurement =>
                    {
                        Code => 'LBS'
                    },
                    Weight => $boxWt
                },
            }
        ],
    }
};

我需要输入的是Package数组中的所有内容。

我有一个生成这样的输出的LoH:

my %carton_specs = 
    (
        25 => {
            boxQty  =>  25,
            boxWt   =>  4,
            boxNo   =>  2
        },
        50 => {
            boxQty  =>  50,
            boxWt   =>  8,
            boxNo   =>  17
        },
);

我需要在Package

中重复匿名数组
{
    PackagingType =>
    {
        Code => '02',
        Description => 'Package'
    },
    PackageWeight =>
    {
        UnitOfMeasurement =>
        {
            Code => 'LBS'
        },
        Weight => $boxWt
    },
},

$boxNo返回的数字的倍数。唯一变化的变量是Weight => $boxWt

请原谅我可能错误的任何内容。我已经和它斗争了2天,我的脑袋正在爆炸。

1 个答案:

答案 0 :(得分:0)

听起来我觉得你正在寻找push以及来自perlreftut的信息(加上可能是.. range operator)。我希望我能正确理解你的规格:

use warnings;
use strict;
use Data::Dump;

my $raterequest = {
    Shipment => {
        Package => [ ],
    } };

my %carton_specs =  (
    25 => { boxQty => 25, boxWt => 4, boxNo =>  2 },
    50 => { boxQty => 50, boxWt => 8, boxNo => 17 },
);

for my $carton (sort keys %carton_specs) {
    for ( 1 .. $carton_specs{$carton}{boxNo} ) {
        push @{ $raterequest->{Shipment}{Package} }, {
            PackagingType => {
                Code => '02',
                Description => 'Package',
            },
            PackageWeight => {
                UnitOfMeasurement => { Code => 'LBS' },
                Weight => $carton_specs{$carton}{boxWt},
            },
        };
    }
}

dd $raterequest;

输出:

{
  Shipment => {
    Package => [
      {
        PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 4 },
        PackagingType => { Code => "02", Description => "Package" },
      },
      {
        PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 4 },
        PackagingType => { Code => "02", Description => "Package" },
      },
      {
        PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 8 },
        PackagingType => { Code => "02", Description => "Package" },
      },
      # ... omit 16 repetitions of the previous hashref ...
    ],
  },
}