简体   繁体   English

在 Perl 中使用 foreach 代替 map 和 grep

[英]Use foreach instead of map and grep in Perl

I am trying to convert the map and grep code totally to foreach loop, but I am facing trouble when I do it.我正在尝试将mapgrep代码完全转换为foreach循环,但是这样做时我遇到了麻烦。

Here is the array code to get the events这是获取事件的数组代码

my @events = [
  {
    Sponsor => ’Saash’,
    Time => ’05:00’,
    Date => ’01-03-2021’,
  },
  {
    Sponsor => ’Saash Tech’,
    Time => ’03:00’,
    Date => ’01-03-2021’,
  },
  {
    Sponsor => ’Saash Technology’,
    Time => ’04:00’,
    Date => ’01-03-2021’,
  },
  {
    Sponsor => ’Saash Techs’,
    Time => ’05:00’,
    Date => ’01-03-2021’,
  },
];

my $time = ’05:00’;

Here is the code used in grep and map:这是 grep 和 map 中使用的代码:

my $data = [
    map
    {
        my $hide;
        if ($_->{'sponsor'}) {
            $_->{'sponsor'} =~ ‘SaAsh’;
            $_->{‘sponsor'} = ‘Saash’ if $_->{’sponsor’} eq 'Saash Techs';
            if ($_->{‘time'} eq ’04:00’ && $_->{‘sponsor'} eq ‘SaAsh’) ) {
                $hide = 1;
            }
        }
        $hide ? () : ( $_ );
    }
    grep { $_->{‘time} =~ $time } @{events}
];

Below is the code where I have converted from grep map to foreach下面是我从 grep map 转换为foreach的代码

my $data;
foreach my $event (@events) {
    if ($event->{‘time’} =~ ’07:00’) {
        my $hide;
        if ($event->{‘sponsor’}) {
            $event->{‘sponsor'} =~ ‘SaAsh’;
            $event->{'sponsor'} = ‘Saash’ if $event->{’sponsor’} eq ‘Saash Techs';
            if ($event->{'time'} eq '04:00' && $event->{'sponsor'} eq ‘SaAsh’) ) {
                $hide = 1;
            }
        }
        $hide ? () : ( $event );
    }
}
$data = [$event];

I am not getting the same output in foreach code when compared to the code of grep and map.与 grep 和 map 的代码相比,我在 foreach 代码中没有得到相同的 output。

Can someone help where am I going wrong?有人可以帮助我哪里出错了吗?

foreach my $event (@events) {
   ...
   $hide ? () : ( $event )
}

$data = [$event];

should be应该

foreach my $event (@events) {
   ...
   push @$data, $hide ? () : ( $event )
}

my @a = map BLOCK LIST; can be written as可以写成

my @a;
for (LIST) {
   push @a, do BLOCK;
}

my @a = grep BLOCK LIST; can be written as可以写成

my @a;
for (LIST) {
   push @a, $_ if do BLOCK;
}

Of course, the do can be removed through simplification and refactoring, and the two loops can be combined in your case.当然, do可以通过简化和重构来去掉,在你的情况下,这两个循环可以结合起来。 After performing the above transformations and some cleanup, we get the following:执行上述转换和一些清理后,我们得到以下信息:

for my $event (@events) {
   $event->{sponsor} = 'Saash'
      if $event->{sponsor}
      && $event->{sponsor} eq 'Saash Techs';
}

my $data = [];
for my $event (@events) {
   next if $event->{time} ne $time;
   next
      if ($event->{sponsor}
      && $event->{sponsor} eq 'SaAsh'
      && $event->{time} eq '04:00';

   push @$data, $event;
}

You were only changing some of the Saash Techs sponsors to Saash in @events , which is why I do the fixes first.您只是在@events中将一些Saash Techs赞助商更改为Saash ,这就是我首先进行修复的原因。 It was a bad practice to change @events surreptitiously like you were doing anyway.像你一样偷偷改变@events是一种不好的做法。

Is this the answer code?这是答案代码吗?


foreach my $event (@events) {
   next if $_->{time} !~ /$time/;

   if ($event->{'sponsor'}) {
      $event->{'sponsor'} =~ ‘SaAsh’;
      $event->{'sponsor'} = 'Saash' if $event->{'sponsor'} eq 'Saash Techs';
      if ($event->{'time'} eq '04:00' && $event->{'sponsor'} eq ‘SaAsh’) ) {
          $hide = 1;
      }
   }

   push @$data, $hide ? () : ( $event );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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