2012-06-23 81 views
0

嘗試從下面的DoxyDocs1.pm文件的詳細 - > doc - > params - > parameters - > name中提取數據;還包括提取一些數據的腳本示例,但無法提取參數 - >參數 - >名稱。取消評論部分的評論以查看數據。從perl中的多個哈希和數組數據結構中提取數據

#!/bin/perl 
use Data::Dumper; 
use warnings; 
require "DoxyDocs1.pm"; 
print "API Content Analyzer\n"; 
&GenerateClassInfo($doxydocs->{classes}); 
sub GenerateClassInfo() 
{ 
    my ($classes) = @_; 
    foreach my $class (@$classes) { 
    print "\nClass name is: $class->{name}\n"; 
    foreach my $pubmeth (@{$class->{public_methods}->{members}}) { 
     print "\n" if exists $pubmeth->{name}; 
     print "\tpublic methods include: $pubmeth->{name}\n"; 
     my ($key, $pmcontent) = each (@{$pubmeth->{detailed}->{doc}}); 
     print "\t\tdescription: $pmcontent->{content}\n" if exists $pmcontent->{content}; 
     # foreach my $pmp (@{$pubmeth->{detailed}->{doc}}) { 
     # print "\t\t"; 
     # print Dumper($pmp); 
     # } 
     print "\t\tkind: $pubmeth->{kind}\n"; 
     print "\t\ttype: $pubmeth->{type}\n" if exists $pubmeth->{type}; 
    } 
    foreach my $privmeth (@{$class->{private_methods}->{members}}) { 
     print "\n" if exists $privmeth->{name}; 
     print "\tprivate methods include: $privmeth->{name}\n"; 
     my ($key, $pmcontent) = each (@{$privmeth->{detailed}->{doc}}); 
     print "\t\tdescription: $pmcontent->{content}\n" if exists $pmcontent->{content}; 
     # foreach my $info (@{$privmeth->{detailed}->{doc}}) { 
     # print "\t\t"; 
     # print Dumper($info); 
     # } 
     print "\t\tkind: $privmeth->{kind}\n"; 
     print "\t\ttype: $privmeth->{type}\n" if exists $privmeth->{type}; 
    }  
    } 
} 

例DoxyDocs1.pm文件

$doxydocs= 
{ 
    classes => [ 
    { 
    name => 'Panoply::Composite', 
    public_methods => { 
    members => [ 
     { 
     kind => 'function', 
     name => 'addChild', 
     virtualness => 'non_virtual', 
     protection => 'public', 
     static => 'no', 
     brief => {}, 
     detailed => { 
      doc => [ 
      { 
       type => 'text', 
       content => 'Add a child to the container ' 
      }, 
      params => [ 
       { 
       parameters => [ 
        { 
        name => 'child' 
        } 
       ], 
       doc => [ 
        { 
        type => 'text', 
        content => 'is the child element to add' 
        } 
       ] 
       } 
      ] 
      ] 
     }, 
     type => 'void', 
     const => 'no', 
     volatile => 'no', 
     parameters => [ 
      { 
      declaration_name => 'child', 
      type => 'Ptr' 
      } 
     ] 
     }, 
     { 
     kind => 'function', 
     name => 'operator<', 
     virtualness => 'non_virtual', 
     protection => 'public', 
     static => 'no', 
     brief => {}, 
     detailed => { 
      doc => [ 
      { 
       type => 'text', 
       content => 'Less than operator' 
      }, 
      { 
       type => 'parbreak' 
      }, 
      params => [ 
       { 
       parameters => [ 
        { 
        name => 'rval' 
        } 
       ], 
       doc => [ 
        { 
        type => 'text', 
        content => 'The ' 
        }, 
        { 
        type => 'url', 
        link => 'classPanoply_1_1Package', 
        content => 'Package' 
        }, 
        { 
        type => 'text', 
        content => ' against which we are comparing this one. ' 
        } 
       ] 
       } 
      ], 
      { 
       return => [ 
       { 
        type => 'text', 
        content => 'true if this.packageID < rval.packageID, false otherwise.' 
       } 
       ] 
      } 
      ] 
     }, 
     type => 'bool', 
     const => 'yes', 
     volatile => 'no', 
     parameters => [ 
      { 
      declaration_name => 'rval', 
      type => 'const Composite &' 
      } 
     ] 
     }, 
    ] 
    }, 
    private_methods => { 
    members => [ 
     { 
     kind => 'function', 
     name => 'addChild', 
     virtualness => 'virtual', 
     protection => 'private', 
     static => 'no', 
     brief => {}, 
     detailed => { 
      doc => [ 
      { 
       type => 'text', 
       content => 'Add a child to the container ' 
      }, 
      params => [ 
       { 
       parameters => [ 
        { 
        name => 'child' 
        } 
       ], 
       doc => [ 
        { 
        type => 'text', 
        content => 'is the child element to add ' 
        } 
       ] 
       }, 
       { 
       parameters => [ 
        { 
        name => 'parent' 
        } 
       ], 
       doc => [ 
        { 
        type => 'parbreak' 
        }, 
        { 
        type => 'text', 
        content => 'is this own parent, except in weak pointer format to avoid a memory leak' 
        } 
       ] 
       } 
      ] 
      ] 
     }, 
     type => 'virtual void', 
     const => 'no', 
     volatile => 'no', 
     parameters => [ 
      { 
      declaration_name => 'child', 
      type => 'Ptr' 
      }, 
      { 
      declaration_name => 'parent', 
      type => 'Ptr' 
      } 
     ] 
     }, 
    ] 
    }, 
    } 
    ] 
}; 
1; 

回答

1

你說你想

detailed -> doc -> params -> parameters -> name 

但缺少很多索引。你想要哪個docparamparameters

detailed -> doc -> ??? -> params -> ??? -> parameters -> ??? -> name 

的語法是:

$member->{detailed}->{doc}->[$i]->{params}->[$j]->{parameters}->[$k]->{name} 

或簡稱:

$member->{detailed}{doc}[$i]{params}[$j]{parameters}[$k]{name} 

如果你想遍歷每個文檔,則params,參數,你可以使用:

my $docs = $member->{detailed}{doc}; 
for my $doc (@$docs) { 
    my $params = $doc->{params}; 
    for my $param (@$params) { 
     my $parameters = $param->{parameters}; 
     for my $parameter (@$parameters) { 
     ... 
     } 
    } 
} 

(爲什麼doc是單數,params是d參數是多個?爲什麼參數有參數???)

+0

感謝您給我更多的洞察這個結構,雖然{params}不是一個哈希引用,並且似乎導致獲取參數名稱「child」的問題。 DoxyDocs文件是從Doxygen自動生成的,我無法控制其結構,可能是該應用程序中的一個錯誤。 –

+0

這是對一個數組的引用,我對它進行了處理。 – ikegami

+0

糟糕,我的意思是說,Data Dumper報告以doc開頭的結構,doc是一個散列鍵,引用一個匿名數組作爲其值。該數組包含3個元素:對匿名散列的引用;字符串「params」;以及對匿名數組的引用。我可以通過以下方法訪問參數child:$ pubmeth - > {detailed} - > {doc} - > [2] - > [0] - > {parameters} - > [0] - > {name};可能有一個我不知道的更好的方法。 –