Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

If exists (hash)

by packetstormer (Monk)
on Oct 06, 2011 at 13:39 UTC ( [id://929984]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow Monks

I have a simple XML result that I am parsing with XML::Simple. One element can have a sub element in some cases so I have the code below to try and check if the variable is in a further down in a hash or not. Data Dumper shows my hash reference is correct. However, I am getting the error futher down when I use diagnostics. Any ideas?

my $web = "http://my-base-url/"; if(exists $xml->{result}->{covers}->{1}->{content}) {$image = "$web$xml->{result}->{covers}->{1}->{content}";} else{$image = "$web$xml->{result}->{covers}"}
ERROR: Can't use string ("/imagegallery2/content/levl/220/") as a HASH ref while "strict refs"

**Updated typo changing :$image = "$web$xml->{results}->{covers}"} to $image = "$web$xml->{result}->{covers}"}

Replies are listed 'Best First'.
Re: If exists (hash)
by choroba (Cardinal) on Oct 06, 2011 at 13:47 UTC
    results ≠ result ("else" line)
Re: If exists (hash)
by Khen1950fx (Canon) on Oct 06, 2011 at 16:03 UTC
    You're using $xml as a scalar element, but exists requires a hash or array element. Also, remember to declare all of your variables. You forgot to declare $image. Here's the way that I would do it:
    #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper::Concise; my %xml; my $image; my $web = 'http://my-base-url/'; if(exists $xml{'result'}{'covers'}{1}{'content'}) { print Dumper($image = "$web$xml{'result'}{'covers'}{1}{'content +'}"); } else { print Dumper($image = "$web$xml{'result'}{'covers'}"); }
Re: If exists (hash)
by choroba (Cardinal) on Oct 06, 2011 at 13:55 UTC
    If you change the original post, you should indicate it somehow. Otherwise, reactions to the previous version of your post become incomprehensible.
      Sorry for the mess here.
      My basic question is how to print a hash key that will only be there in some records. I thought the "if exists" might work but it clearly doesn't.

        My basic question is how to print a hash key that will only be there in some records. I thought the "if exists" might work but it clearly doesn't.

        Um, your code fragment is incomplete, it does not demonstrate "if exist" not working

        See Access Hashes of Hashes etc. and Re^2: Unique value count in hash not working properly

        $ perl -Mstrict -e " my $foo = { 1, 1 }; exists $foo->{1}{2} " Can't use string ("1") as a HASH ref while "strict refs" in use at -e +line 1.
Re: If exists (hash)
by jwkrahn (Abbot) on Oct 06, 2011 at 20:21 UTC
    {$image = "$web$xml->{result}->{covers}->{1}->{content}";} else{$image = "$web$xml->{result}->{covers}"}

    Have you read the FAQ:

    What's wrong with always quoting "$vars"?

    Do you really need to make a copy of $web$xml->{result}->{covers}->{1}->{content} or $web$xml->{result}->{covers}?

Re: If exists (hash)
by Anonymous Monk on Oct 06, 2011 at 13:48 UTC
      I am not sure its the accessing of the hash that is the problem. The value of the string in the error is correct. That is, it is pulling the correct information from the hash. My problem appears to be the code I am using somewhere around that!

        I am not sure its the accessing of the hash that is the problem.

        You don't have to be, perl is telling you it is the problem -- perl is right

        My problem appears to be the code I am using somewhere around that!

        Yes, but your question isn't effective -- How do I post a question effectively?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-03-19 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found