[英]how to combine two next if together
我有一个在文件列表上运行的脚本来进行一些更改,其中的每个文件都有一个调用事件,调用事件详细信息包含 4 个元素,所以我只想在这里对其中 2 个进行更改,我我坚持如何在一个循环中将两个next if
一个组合起来,在这里我使用了 2 个循环来完成这项工作,但需要更多时间,有没有关于如何做到这一点的想法?
my $calleventtag = $struct->{'transferBatch'}->{'callEventDetails'};
my @indexes = reverse (grep { exists $calleventtag->[$_]->{'supplServiceEvent'} } 0..$#$calleventtag);
my $sup_event_cnt = $#indexes;
foreach my $index (@indexes)
{
splice (@$calleventtag , $index,1);
}
foreach (0..$#$calleventtag)
{
next if ( ! exists $calleventtag->[$_]->{'mobileOriginatedCall'}) ;
if ( exists $calleventtag->[$_]->{'mobileOriginatedCall'}->{'basicCallInformation'}->{'destinationNetwork'} )
{
delete $calleventtag->[$_]->{'mobileOriginatedCall'}->{'basicCallInformation'}->{'destinationNetwork'};
}
if ( exists $calleventtag->[$_]->{'mobileOriginatedCall'}->{'basicCallInformation'}->{'chargeableSubscriber'}->{'simChargeableSubscriber'}->{'msisdn'}
&& $calleventtag->[$_]->{'mobileOriginatedCall'}->{'basicCallInformation'}->{'chargeableSubscriber'}->{'simChargeableSubscriber'}->{'msisdn'} !~ m/^96279/
)
{
delete $calleventtag->[$_]->{'mobileOriginatedCall'}->{'basicCallInformation'}->{'chargeableSubscriber'}->{'simChargeableSubscriber'}->{'msisdn'};
}
}
foreach (0..$#$calleventtag)
{
next if ( ! exists $calleventtag->[$_]->{'gprsCall'});
if ( exists $calleventtag->[$_]->{'gprsCall'}->{'gprsBasicCallInformation'}->{'gprsDestination'}->{'accessPointNameOI'} )
{
delete $calleventtag->[$_]->{'gprsCall'}->{'gprsBasicCallInformation'}->{'gprsDestination'}->{'accessPointNameOI'};
}
}
for (...) {
next if ...;
...
}
也可以写成
for (...) {
if (!...) {
...
}
}
您可以使用以下内容:
use Data::Diver qw( Dive );
my $call_event_details = Dive($struct, qw( transferBatch callEventDetails ));
for my $call_event_detail (@$call_event_details) {
next if !$call_event_detail->{supplServiceEvent};
if ( my $bci = Dive($call_event_detail, qw( mobileOriginatedCall basicCallInformation )) ) {
delete $bci->{destinationNetwork};
if ( my $scs = $bci->{simChargeableSubscriber} ) {
my $msisdc = $scs->{msisdn};
delete $scs->{msisdn} if $msisdc && $msisdc !~ /^96279/;
}
}
if ( my $dest = Dive($call_event_detail, qw( gprsCall gprsBasicCallInformation gprsDestination )) ) {
delete $dest->{accessPointNameOI};
}
}
笔记:
$hash->{'foo'}
可以写成$hash->{foo}
。->
两个索引之间不需要。 例如, $hash->{foo}->{bar}
可以写成$hash->{foo}{bar}
。exists
来检查是否有引用; 您可以使用简单的真值测试,因为引用始终为真。 [BUG FIX] $hash->{foo}{bar}
可以自动激活$hash->{foo}
(导致分配给它的引用),因此您检查内容是否存在的测试实际上可能会导致创建内容. 要解决此问题,您可以更换
if ($hash->{foo}{bar})
和
if ($hash->{foo} && $hash->{foo}{bar})
或者
if (Dive($hash, qw( foo bar )))
重复使用相同的长索引链( ->{foo}{bar}{baz}
)很容易出错。
$calleventtag
作为包含callEventDetails
节点的变量名?delete
可以传递一个不存在的元素。grep
是一个不错的选择,但splice
不是。 你应该使用过: $calleventtag = [ grep { ... } @$calleventtag ];
. 我把支票移到了循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.