问题描述
我试图使用perl代码从下面的命令中存储信息,
I tried to store information from below command using perl code,
假定此命令的输出如下,
Assume the output from this command as below,
AMQ8409: Display Queue details.
QUEUE(SYSTEM.RETAINED.PUB.QUEUE) TYPE(QLOCAL)
ACCTQ(QMGR) ALTDATE(2016-08-01)
ALTTIME(18.33.20) BOQNAME( )
CLWLPRTY(0) CLWLRANK(0)
CLWLUSEQ(QMGR) CRDATE(2016-08-01)
CRTIME(18.32.37) CURDEPTH(2)
MAXDEPTH(999999999) MAXMSGL(104857600)
AMQ8409: Display Queue details.
QUEUE(SYSTEM.SELECTION.EVALUATION.QUEUE)
TYPE(QLOCAL) ACCTQ(QMGR)
ALTDATE(2016-08-01) ALTTIME(18.33.20)
BOQNAME( ) BOTHRESH(0)
CLWLRANK(0) CLWLUSEQ(QMGR)
CRDATE(2016-08-01) CRTIME(18.32.37)
CURDEPTH(0) CUSTOM( )
IPPROCS(0) MAXDEPTH(999999999)
AMQ8409: Display Queue details.
QUEUE(SYSTEM.SELECTION.VALIDATION.QUEUE)
TYPE(QLOCAL) ACCTQ(QMGR)
ALTDATE(2016-08-01) ALTTIME(18.33.20)
BOQNAME( ) BOTHRESH(0)
CLWLRANK(0) CLWLUSEQ(QMGR)
CRDATE(2016-08-01) CRTIME(18.32.37)
CURDEPTH(0) CUSTOM( )
IPPROCS(0) MAXDEPTH(999999999)
因此,对于每个输出(从中我们可以看到3个),我编写了一个代码来存储关于每个MQ队列名称的QUEUE,CURDEPTH和MAXDEPTH的信息.
So, for each of this output (from we can see there are 3), I wrote a code to store information about QUEUE, CURDEPTH and MAXDEPTH for each MQ queue name.
我的代码如下,
#!/usr/bin/perl
use warnings;
use strict;
my %QM;
my $qm = 'MQ_NAME';
open (CHS_OUT, "echo 'dis ql(*) all'|runmqsc $qm|");
while (<CHS_OUT>) {
my ($QUEUE, $QName) = / ( QUEUE ) \( ( [^)]+ ) /x;
if ( my ($item, $value)
= / ( CURDEPTH | MAXDEPTH ) \( ( [^)]+ ) /x )
{
$QM{$QName}{$item} = $value;
}
}
foreach my $Que (sort keys %QM) {
foreach my $v (keys %{ $QM{$Que} }) {
print "$Que : $v = $QM{$Que}{$v} \n";
}
}
我已经尝试将$ QName设置为全局,但仍然无法解决问题,此代码仍然存在错误.
I already try to make the $QName as global still cannot help, there are still error from this code.
我的代码的输出:
Use of uninitialized value $QName in hash element at mq_queue_5.pl line 25, <CHS_OUT> line 7353.
Use of uninitialized value $QName in hash element at mq_queue_5.pl line 25, <CHS_OUT> line 7361.
Use of uninitialized value $QName in hash element at mq_queue_5.pl line 25, <CHS_OUT> line 7384.
Use of uninitialized value $QName in hash element at mq_queue_5.pl line 25, <CHS_OUT> line 7392.
: CURDEPTH = 0
: MAXDEPTH = 999999999
我已经停留了好几天,已经做了很多更改,但仍然无济于事.
I already stuck for multiple days, already make a lot of changes but still does'nt help.
推荐答案
您必须检查QName是否匹配并记住它:
You have to check if the QName is matched and remember it:
#!/usr/bin/perl
use warnings;
use strict;
my %QM;
my $qm = 'MQ_NAME';
open (CHS_OUT, "result.txt");
my $QName;
while (<CHS_OUT>) {
my ($QUEUE, $QName1) = / ( QUEUE ) \( ( [^)]+ ) /x;
if ($QName1) {
$QName=$QName1;
}
if ( my ($item, $value)
= / ( CURDEPTH | MAXDEPTH ) \( ( [^)]+ ) /x )
{
$QM{$QName}{$item} = $value;
}
}
foreach my $Que (sort keys %QM) {
foreach my $v (keys %{ $QM{$Que} }) {
print "$Que : $v = $QM{$Que}{$v} \n";
}
}
输出:
SYSTEM.RETAINED.PUB.QUEUE : CURDEPTH = 2
SYSTEM.RETAINED.PUB.QUEUE : MAXDEPTH = 999999999
SYSTEM.SELECTION.EVALUATION.QUEUE : CURDEPTH = 0
SYSTEM.SELECTION.EVALUATION.QUEUE : MAXDEPTH = 999999999
SYSTEM.SELECTION.VALIDATION.QUEUE : MAXDEPTH = 999999999
SYSTEM.SELECTION.VALIDATION.QUEUE : CURDEPTH = 0
这篇关于散列哈希Perl代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!