Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Not a HASH reference

by pdahal (Acolyte)
on Mar 22, 2017 at 16:06 UTC ( [id://1185472]=perlquestion: print w/replies, xml ) Need Help??

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

I am getting following error in perl. "Not a HASH reference at tocsvfd.pl line 31". the code.Below is the code through where I am getting this error. How can I correct it? The last line of the code is line 32
my $src = dir("C:/Perl/pubmed/Prasuna/DM/Database/Drugbank/Split"); #Check files in folder my @txt_files = $src->children; for my $txt_file ( @txt_files ) { my $in = $txt_file->openr; my $parser = new XML::Simple; my $tree = $parser->XMLin($in); $alldrug = $tree->{drug}; #print pp($alldrug); @allkeys = keys % $alldrug;

Replies are listed 'Best First'.
Re: Not a HASH reference
by kennethk (Abbot) on Mar 22, 2017 at 16:43 UTC
    Assuming line 31 is as you say, this means $alldrug does not contain hashref. If we trace that back, its value is assigned on line 29. That line looks fine (XMLin should return a data structure that mimics for XML), so that implies that your data structure is not as you expect. Are you sure that your $tree->{drug} branch contains a hash ref and not, for example, an array ref? To interrogate your data structure, try following the directions in Basic debugging checklist and use Data::Dumper or Data::Dump (It looks like you tried that because of your commented #print pp($alldrug); line, but then again you can't get that error if $alldrug contains what you seem to expect).

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Not a HASH reference
by haukex (Archbishop) on Mar 22, 2017 at 16:38 UTC

    So do I understand correctly that @allkeys = keys %$alldrug; is the line that is throwing the error? What does print pp($alldrug); show, and if that shows undef, what does print pp($tree); show?

    Also, note that there are much better modules to parse XML than XML::Simple! I would recommend either XML::LibXML or XML::Twig.

Re: Not a HASH reference
by Anonymous Monk on Mar 23, 2017 at 00:07 UTC

    Hi,

    xpath is easy with xpather.pl

    use XML::LibXML; my $dom = XML::LibXML->new(qw/ recover 2 /)->load_xml( IO => $in ); for my $key ( $dom->findnodes( q{ /drug/* } ) ){ print $key->nodeName, "\n", $key->nodePath, "\n", $key->textValue, "###\n"; }

    If /drug doesn't work then  *[local-name()='drug' ] will

    Or even /x:drug if you use sub XML::LibXML::Node::F

Re: Not a HASH reference
by Anonymous Monk on Mar 22, 2017 at 16:09 UTC
    my $tree = $parser->XMLin($in);
    $in is not being opened. Check the return value. Check for errors.
      $in is not being opened.

      That seems to be very unlikely. Based on dir(...), ->children and ->openr, I am guessing this is Path::Class. Its openr throws an error if the file couldn't be opened. Even if $in were undef, there would need to be a file named tocsvfd.xml in the same path as the script, otherwise XMLin would throw an error.

      how do I open $in?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found