2017-04-25 157 views
1

我試圖從下面的命令使用Perl代碼存儲信息,散列在散列Perl代碼問題

DIS QL(*)所有的'|的runmqsc MQ_NAME

假設該命令的輸出下面,

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個),我寫了一個代碼來存儲隊列中,CURDEPTH和MAXDEPTH信息爲每個MQ隊列名稱。

我的代碼如下,

#!/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全球仍不能幫助,還有來自該代碼錯誤。從我的代碼

輸出:

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 

我已經被困多天,已經做出了很大的變化,但仍然一點兒也不幫助。

+1

看起來像某個時候'$ QName'沒有在您正則表達式匹配。 – Jens

回答

3

你必須檢查的QName匹配並記住它:

#!/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"; 
     } 
} 

OUTPUT:

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 
+0

@Downvoter:請解釋,所以我可以改善我的答案 – Jens