我有这个数据结构:
$VAR1 = {
'cdr' => [
{
'networkInfo' => {
'extPayer' => '50351851444',
'eventSource' => 'J@nus',
'sequenceNumber' => '0',
'connectionId' => '0050351851444',
'payer' => '50351851444',
'status' => '1',
'mvnoId' => '2',
'currentStatus' => '2',
'eventReferenceNumber' => '1510141545000003101',
'sessionId' => '1510143920000000011',
'errorCode' => '0',
'previousStatus' => '1',
'eventDateTime' => '20151014154533',
'requestType' => '51',
'actionDateTime' => '20151014154533',
'exponent' => '-6'
},
'payerInfo' => {
'newPayerStatus' => '2',
'noOfPayerValidity' => '0',
'prevPayerStatus' => '1',
'noOfWalletsUsed' => '0',
'baseWalletBalance' => '0'
},
'pricedInfo' => {
'totalUsedUnits' => '0',
'taxingOptionUsed' => '0',
'totalSalePrice' => '0',
'totalTax' => '0',
'totalCostPrice' => '0',
'chargeSummary' => {
'dimensionSummary' => {
'dimensionId' => '0',
'componentSummary' => {
'totalSalePrice' => '0',
'totalCostPrice' => '0',
'componentId' => '0',
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
}
},
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
},
{
'networkInfo' => {
'extPayer' => '50351851444',
'eventSource' => 'J@nus',
'sequenceNumber' => '0',
'connectionId' => '0050351851444',
'payer' => '50351851444',
'status' => '1',
'mvnoId' => '2',
'currentStatus' => '2',
'eventReferenceNumber' => '1510141545000003101',
'sessionId' => '1510143920000000021',
'errorCode' => '0',
'previousStatus' => '2',
'eventDateTime' => '20151014154535',
'requestType' => '53',
'actionDateTime' => '20151014154535',
'exponent' => '-6'
},
'payerInfo' => {
'newPayerStatus' => '2',
'noOfPayerValidity' => '0',
'prevPayerStatus' => '2',
'noOfWalletsUsed' => '0',
'baseWalletBalance' => '0'
},
'pricedInfo' => {
'totalUsedUnits' => '0',
'taxingOptionUsed' => '0',
'totalSalePrice' => '0',
'totalTax' => '0',
'totalCostPrice' => '0',
'chargeSummary' => {
'dimensionSummary' => {
'dimensionId' => '0',
'componentSummary' => {
'totalSalePrice' => '0',
'totalCostPrice' => '0',
'componentId' => '0',
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
}
},
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
},
{
'networkInfo' => {
'extPayer' => '50351851444',
'eventSource' => 'J@nus',
'sequenceNumber' => '0',
'connectionId' => '0',
'payer' => '50351851444',
'status' => '1',
'mvnoId' => '2',
'currentStatus' => '2',
'eventReferenceNumber' => '0',
'sessionId' => '2015101400000335940',
'errorCode' => '0',
'previousStatus' => '2',
'eventDateTime' => '20151014154540',
'requestType' => '3',
'actionDateTime' => '20151014154535',
'exponent' => '-6',
'forceDebit' => '0'
},
'payerInfo' => {
'newPayerStatus' => '2',
'noOfPayerValidity' => '0',
'prevPayerStatus' => '2',
'noOfWalletsUsed' => '0',
'baseWalletBalance' => '9998000000'
},
'pricedInfo' => {
'totalUsedUnits' => '0',
'taxingOptionUsed' => '0',
'totalSalePrice' => '0',
'totalTax' => '0',
'totalCostPrice' => '0',
'chargeSummary' => {
'dimensionSummary' => {
'dimensionId' => '0',
'componentSummary' => {
'totalSalePrice' => '0',
'totalCostPrice' => '0',
'componentId' => '0',
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
}
},
'totalDiscountAmount' => '0',
'bucketsUsed' => '0'
}
},
{
'networkInfo' => {
'extPayer' => '50351851444',
'instanceId' => '0',
'imei' => '0',
'passwordId' => '0',
'networkUsageIdentifier' => '67060224951',
'ReleaseCause' => '01144',
'spendControlId' => '0',
'defaultPayer' => '50351851444',
'sessionId' => '1510141545000003101',
'chargingGroupNumberDiscountPercent' => '0',
'errorCode' => '0',
'calledPartyAddress' => '0050399051801',
'callingPartyAddress' => '0050351851444',
'semiAutoCollectClassId' => '0',
'destinationZoneCode' => '1021',
'requestType' => '1001',
'redirectingPartyId' => '',
'ApplicationServer' => '7060261771',
'chargingGroupDiscountPercent' => '0',
'timeZone' => '+1Q',
'chargingGroupClass' => '0',
'ccRequestType' => '3',
'defaultExtPayer' => '50351851444',
'carrierCode' => '',
'PrefixDialled' => '',
'dialingType' => '99',
'connectionType' => '0',
'mcc' => '706',
'locationInfo' => '2495100001',
'originalCalledPartyId' => '',
'specialNumberClassId' => '0',
'eventDateTime' => '20151014154535',
'bearerService' => '0',
'smscRefernceNumber' => '',
'sequenceNumber' => '0',
'redirectionInformation' => '',
'unitType' => '0',
'connectTime' => '20151014154535',
'holidayCode' => '0',
'imsi' => '51851444',
'serviceIdentifier' => '0',
'ratingZoneType' => '1',
'mscAddress' => '',
'enterpriseCall' => '0',
'isTollFree' => '0',
'specialLocationClassId' => '0',
'routingNumber' => '',
'eventType' => '0',
'payerType' => '0',
'connectionId' => '0050351851444',
'payer' => '50351851444',
'subscriberClass' => '0',
'chargingGroupId' => '0',
'callReferenceNumber' => '3031303032303031',
'mvnoId' => '2',
'originatingNetworkId' => '1021',
'callType' => '1',
'dialDigitId' => '1041',
'actionDateTime' => '20151014154535',
'callTerminationCause' => '1',
'vpnGroupId' => '0',
'mnc' => '02',
'exponent' => '-6'
},
'payerInfo' => {
'newPayerStatus' => '2',
'noOfPayerValidity' => '0',
'prevPayerStatus' => '2',
'walletDetails' => {
'walletId' => '1',
'prevWalletBalance' => '10000000000',
'prevExpiryDate' => '20991231',
'expiryFlag' => '0',
'newWalletBalance' => '9998000000',
'newExpiryDate' => '20991231'
},
'noOfWalletsUsed' => '1',
'baseWalletBalance' => '9998000000'
},
'pricedInfo' => {
'totalUsedUnits' => '120',
'totalCostPrice' => '2000000',
'bucket' => {
'rateId' => '1002',
'discountPercentage' => '0',
'startUnits' => '1',
'exchangeRate' => '1000000',
'taxFormulaId' => '1002',
'componentId' => '0',
'taxWalletId' => '1',
'currencyRateId' => '1',
'discountArticleId' => '1001',
'walletId' => '1',
'salePrice' => '2000000',
'discountAmount' => '0',
'TaxDetail' => {
'Tax' => {
'amount' => '0',
'count' => '1',
'article' => '1009'
}
},
'usedUnits' => '120',
'endUnits' => '120',
'taxApplied' => '0',
'debitedValue' => '2000000',
'costPrice' => '2000000',
'dimensionId' => '0',
'serviceKey' => '0',
'chargeArticleId' => '1',
'tariffId' => '1026'
},
'taxingOptionUsed' => '0',
'totalTax' => '0',
'totalSalePrice' => '2000000',
'chargeSummary' => {
'dimensionSummary' => {
'dimensionId' => '0',
'componentSummary' => {
'totalSalePrice' => '2000000',
'totalCostPrice' => '2000000',
'componentId' => '0',
'totalDiscountAmount' => '0',
'bucketsUsed' => '1'
}
}
},
'bucketsUsed' => '1',
'totalDiscountAmount' => '0'
}
}
]
};
我想从每个requestType
参数中检索networkInfo
。我们有3到4个网络信息参数,我只想检索requestType=1001
。我怎样才能做到这一点?
我正在使用XML :: Simple,我只想坚持使用它。
答案 0 :(得分:4)
严肃地看。 XML::Simple
说的原因是:
不鼓励在新代码中使用此模块。
是因为使用xpath
表达式,这种任务实际上非常简单。
稍微猜测一下你的源XML(给我一些示例XML,我可以给你一个更准确的答案),如果requestType
是一个属性,那就是:
use XML::Twig;
my $twig = XML::Twig -> parsefile ('your_stuff.xml');
my $netinf = $twig -> get_xpath('//networkInfo[@requestType="1001"]',0);
或者如果它是一个节点,你想要所有节点:
foreach my $requestType ( $twig -> get_xpath('//networkInfo/requestType') ) {
print $requestType -> text;
}
使用上面的数据结构,我们不是在谈论XML问题,而是在谈论数据结构遍历问题。而且您的数据结构庞大而复杂。
如果你想走这条路,那么诀窍就是 - 计算Data::Dumper
中的括号,因为它们告诉你什么时候你得到一个哈希(ref)或一个数组(ref)。
你可以得到你想要的东西:
foreach my $hashref ( @{$VAR1->{cdr}} ) {
print $hashref->{networkInfo}->{requestType},"\n";
}
但这对于维护和使用来说会很麻烦,所以为了您未来的维护程序员,请不要使用XML::Simple
。
答案 1 :(得分:1)
也许你需要数量的元素?
my @elems = grep { 1001 == $_ }
map { $_->{networkInfo}->{requestType} }
@{ $VAR1->{cdr} };
my $count = scalar @elems;