在数组列表中搜索模式,如果在perl中找到模式,则选择一个变量

时间:2014-01-08 12:24:50

标签: perl

我正在尝试从数组中获取字符串但是在获取所需输出时遇到了一些麻烦。我尝试使用正则表达式,如果条件,但这不是很有帮助 我的阵列

com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.componentLevel
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.queueName = JP.AP.ADAPTER.REQUEST
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.resetBrowseTimeout
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.securityProfileName
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.serializationToken
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.topicProperty
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.ADAPTER.REQUEST.validateMaster
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.queueManagerName
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.queueName = JP.AP.SERVICE.REQUEST
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.replyToQ
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.replyToQMgr
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.securityProfileName
com..JP.ap.adapter.AccountPayableCopybookAdapter#JP.AP.SERVICE.REQUEST.validateMaster
com..JP.ap.adapter.AccountPayableCopybookAdapter#TransformInvoice_COBOL_TO_XML.dataSource
com..JP.ap.adapter.AccountPayableCopybookAdapter#TransformInvoice_COBOL_TO_XML.validateMaster
com..JP.common.exceptionhandler.FireAndForget_FlatFile#BuildErrorNotification.dataSource
com..JP.common.exceptionhandler.FireAndForget_FlatFile#BuildErrorNotification.validateMaster
com..JP.common.exceptionhandler.FireAndForget_FlatFile#IsLastAttempt?.dataSource
com..JP.common.exceptionhandler.FireAndForget_FlatFile#JP.EXCEPTION.queueManagerName
com..JP.common.exceptionhandler.FireAndForget_FlatFile#JP.EXCEPTION.queueName = JP.EXCEPTION
com..JP.ap.AccountPayableService#providerPolicySetBindings
com..JP.ap.AccountPayableService#securityProfileName
com..JP.ap.AccountPayableService#monitoringProfile
com..JP.ap.AccountPayableACH#JP.AP.ACH.REQUEST.queueManagerName
com..JP.ap.AccountPayableACH#JP.AP.ACH.REQUEST.queueName = JP.AP.ACH.REQUEST
com..JP.ap.AccountPayableService#FireAndForget_SOAP.AlertEmailList = 
com..JP.ap.AccountPayableService#FireAndForget_SOAP.BackOutThreshold = 1
com..JP.ap.AccountPayableService#FireAndForget_SOAP.LogLevel = ERROR
com..JP.ap.AccountPayableService#FireAndForget_SOAP.MaxPerInterval = 0
com..JP.ap.AccountPayableService#FireAndForget_SOAP.NotificationInterval = 0
com..JP.ap.AccountPayableService#JP.AP.SERVICE.FAILURE.queueManagerName
com..JP.ap.AccountPayableService#JP.AP.SERVICE.FAILURE.queueName = JP.AP.SERVICE.FAILURE

现在我只需要数组中“queueName =”之后的字符串,我希望它们是uniq并排序。

JP.AP.ACH.REQUEST
JP.AP.ADAPTER.REQUEST
JP.AP.SERVICE.FAILURE
JP.AP.SERVICE.REQUEST
JP.EXCEPTION

我试过了:

foreach $_ (@array){
  if (/ .* queueName = '(.*?)'/){
    print ("$1\n");
  }
} 

2 个答案:

答案 0 :(得分:0)

我认为您希望queueName =的数组my @rray = # ...however you get it my %uniques = map { my $bite = MOREREGEX on $_; $bite => 1 } grep(SOMEREGEX,@rray); return sort(keys %uniques) 获取已过滤的数组,然后grep将这些过滤后的结果转换为哈希值,以便只获取最后的内容并抛出重复数据,然后从哈希中取出密钥并map

这看起来大致像

grep

编辑:虽然我认为使用my @rray = # ...however you get it my %uniques = map { my ($junk,$want) = split('queueName = ',$_); $want => 1 } @rray; return sort(keys %uniques) 使得程序员打算更清楚一点(在我的解决方案中),可以整合@Bruce回答的内容以避免使用它

$want

因为实际上没有发生拆分时,''undef keys '' 拉出。

错误修复:实际上shift键位于此处,但它总是在排序后首先显示,并且可以{{1}}关闭。

答案 1 :(得分:0)

my %uniques;
my $string_pattern = "queueName = ";
foreach (@string_array)
{
        if($_ =~ m/$string_pattern/)
        {
                my ($junk, $pattern) = split($string_pattern, $_);
                $uniques{$pattern} = 1;
        }
}

foreach (sort keys %uniques)
{
        print $_."\n";
}