简体   繁体   English

如果在一起,如何将两者结合起来

[英]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:笔记:

  • The quotes around string literals aren't needed in hash indexes if the string is valid valid identifier.如果字符串是有效的有效标识符,则哈希索引中不需要字符串文字周围的引号。 For example, $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}
  • If a hash element is either a reference or doesn't exist, you don't need to use 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} )很容易出错。

  • It's best to use plural names for arrays.最好对数组使用复数名称。 First, it's more descriptive, but it also makes choosing names for loop variables easier.首先,它更具描述性,但也使得为循环变量选择名称变得更加容易。
  • Speaking of variable names, why would use $calleventtag for the name of the variable containing callEventDetails nodes?说到变量名,为什么要使用$calleventtag作为包含callEventDetails节点的变量名?
  • You don't need to check if a hash element exists before trying to delete it;在尝试删除散列元素之前,您不需要检查它是否存在; delete can be passed an element that doesn't exist. delete可以传递一个不存在的元素。
  • No need to loop over the indexes of an array if you don't need the indexes.如果不需要索引,则无需遍历数组的索引。
  • 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM