grep一次在perl数组中的多个模式

时间:2016-07-14 13:30:35

标签: arrays regex perl

下面是实际在perl数组中找到模式的代码。

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
    throws IOException, ServletException
{
    HttpSession session = null;

    if(request instanceof HttpServletRequest)
        session = ((HttpServletRequest)request).getSession(false);

    // Start the clock
    long elapsed = System.currentTimeMillis();

    // Use try/finally because the request can throw an exception;
    // We still want to capture the usage-time even if an error
    // occurs.
    try {
        chain.doFilter(request, response);
    } finally {
        // Stop the clock
        elapsed = System.currentTimeMillis() - elapsed;

        // Store the cumulative usage time in the session
        if(null != session) {
            Long usage = (Long)session.getAttribute("usage");
            if(null != usage)
                elapsed = elapsed + usage.getLong();
            session.setAttribute("usage", Long.valueOf(elapsed));
        }
    }
}

但我想一次在两个索引中搜索两种模式

 my $isAvailable = grep { $_->[0] eq '12345' } {$filteredTableEntriesMap{$REPORT_PART1}} ;

这是地图的表示方式

 my $isWiuAvailable = grep { $_->[0] eq '12345' }     @{$filteredTableEntriesMap{$REPORT_PART1}} ;
 my $isBsAvailable  = grep { $_->[1] eq '6789' } @{$filteredTableEntriesMap{$REPORT_PART1}} ;

我希望匹配一个在索引1和索引2中具有这两个条目的数组

由于

1 个答案:

答案 0 :(得分:2)

您可以将这两个条件合并为一个表达式。

my @found = grep { $_->[0] eq '12345' && $_->[1] eq '6789' }
   @{$filteredTableEntriesMap{$REPORT_PART1}};

{} grep内的内容基本上是一个子程序。如果您想在$_结果中保留@found,只要您返回真值,就可以在此处执行任意操作。