Perl生成复杂记录的哈希



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

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

请原谅我可能错了的任何东西。我已经与这个战斗了两天,我的头正在爆炸。

在我看来,您正在寻找push加上Perlreftut的信息(加上..范围运算符(。我希望我正确理解您的规格:

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 ...
    ],
  },
}

相关内容

  • 没有找到相关文章

最新更新