[英]how to combine two next if together
I have a script that run on the list of files to do some of the changes, each file of them has a call event and the call event details contains 4 elements so i just want to do the changes on 2 of them only here I'm stuck with how to combine two next if
in one loop, here I have used 2 loops to do the job but it takes more time , is there any idea about how to do that ?我有一个在文件列表上运行的脚本来进行一些更改,其中的每个文件都有一个调用事件,调用事件详细信息包含 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 ...;
...
}
can also be written as也可以写成
for (...) {
if (!...) {
...
}
}
You could use the following:您可以使用以下内容:
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};
}
}
Notes:笔记:
$hash->{'foo'}
can be written as $hash->{foo}
.例如, $hash->{'foo'}
可以写成$hash->{foo}
。->
isn't needed between two indexes. ->
两个索引之间不需要。 For example, $hash->{foo}->{bar}
can be written as $hash->{foo}{bar}
.例如, $hash->{foo}->{bar}
可以写成$hash->{foo}{bar}
。exists
to check if you have a reference;如果散列元素是引用或不存在,则无需使用exists
来检查是否有引用; you can use a simple truth test since references are always true.您可以使用简单的真值测试,因为引用始终为真。 [BUG FIX] $hash->{foo}{bar}
can autovivify $hash->{foo}
(cause a reference to be assigned to it), so your tests to check if stuff exists could actually be causing things to be created. [BUG FIX] $hash->{foo}{bar}
可以自动激活$hash->{foo}
(导致分配给它的引用),因此您检查内容是否存在的测试实际上可能会导致创建内容. To fix this, you can replace要解决此问题,您可以更换
if ($hash->{foo}{bar})
with和
if ($hash->{foo} && $hash->{foo}{bar})
or或者
if (Dive($hash, qw( foo bar )))
Using the same long chain of indexes ( ->{foo}{bar}{baz}
) repeatedly is error prone.重复使用相同的长索引链( ->{foo}{bar}{baz}
)很容易出错。
$calleventtag
for the name of the variable containing callEventDetails
nodes?说到变量名,为什么要使用$calleventtag
作为包含callEventDetails
节点的变量名?delete
can be passed an element that doesn't exist. delete
可以传递一个不存在的元素。grep
was a good choice, but splice
was not. grep
是一个不错的选择,但splice
不是。 You should have used: $calleventtag = [ grep { ... } @$calleventtag ];
你应该使用过: $calleventtag = [ grep { ... } @$calleventtag ];
. . I moved the check into the loop.我把支票移到了循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.