Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

dereferencing multiple hash array structure

by jccunning (Acolyte)
on Jun 23, 2012 at 02:54 UTC ( [id://977931]=perlquestion: print w/replies, xml ) Need Help??

jccunning has asked for the wisdom of the Perl Monks concerning the following question:

Trying to extract info from an array or hash that is inside multiple hash and array structure in a perl module generated by doxygen. I can extract most everything except what is inside detailed -> doc -> params -> parameters in following DoxyDocs1.pm

Uncomment commented sections to see data trying to extract

Including small sample of code

#!/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->{t +ype}; } 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}->{do +c}}); 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 th +is one. ' } ] } ], { return => [ { type => 'text', content => 'true if this.packageID < rval.packag +eID, 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;

Replies are listed 'Best First'.
Re: dereferencing multiple hash array structure
by GrandFather (Saint) on Jun 23, 2012 at 03:09 UTC

    Prior to Perl 5.12 each expects a hash but you are giving it an array. From Perl 5.12 onward the code is syntactically correct, but probably doesn't do what you expect.

    True laziness is hard work
Re: dereferencing multiple hash array structure
by Athanasius (Archbishop) on Jun 23, 2012 at 03:40 UTC

    Here is an extract from the data structure in DoxyDocs1.pm, re-formatted for greater readability and with one => re-written as quote + comma (see Comma Operator in perlop):

    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' } ] } ] ] },

    As you can see, doc is a hash key with a reference to an anonymous array as its value. This array contains 3 elements: a reference to an anonymous hash; the string "params"; and a reference to an anonymous array. As GrandFather says, this is syntactically OK, but it is almost certainly not what was intended — which is why you’re unable to extract the data you want. You will need to re-consider the way this data structure is generated.

    Update: Using prototypes on subroutines is usually a bad idea, unless you have a strong reason to. Better to remove the trailing parentheses from the definition of sub GenerateClassInfo; then call it without the & prefix.

    HTH,

    Athanasius <°(((><contra mundum

      Using prototypes on subroutines is usually a bad idea...

      Further to Athanasius's point: The function  GenerateClassInfo is prototyped to take zero arguments, but is defined as operating on a single argument. The only way this function can work as defined is to defeat the prototyping mechanism by calling the function with the  & sigil. But what's the point of all that? Wasted motion of that sort is one of the reasons prototypes have a bad reputation. For far more, see Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen. (Update: Furthermore, the  GenerateClassInfo function is called before it is either defined or declared, i.e., before its prototype is available for checking. This would have generated a warning had the function not been called with the  & sigil.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977931]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found