Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

XML::Simple isn't simple!

by r.joseph (Hermit)
on Aug 17, 2001 at 05:30 UTC ( #105594=perlquestion: print w/replies, xml ) Need Help??
r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Given the following small XML file:

<operating_systems> <os score="10" name="Linux" /> <os score="7" name="BeOS" /> <os score="-999999999" name="Windows 9x" /> </operating_systems>

...and the following code snippet:

#!/usr/bin/perl use warnings; use XML::Simple; use Data::Dumper; my $file = './os.xml'; my $ref = XMLin($file); print $ref{'os'}->{'Linux'}->{'score'};

...with the resulting data structure in $ref looking like:

$VAR1 = { 'os' => { 'BeOS' => { 'score' => '7' }, 'Windows 9x' => { 'score' => '-999999999' }, 'Linux' => { 'score' => '10' } } };

one would expect that the score value for Linux would be printed, which should be, in this case, 10.

Then why oh why does this script instead print nothing at all?!

Oh XML::Simple, why can't you just be SIMPLE?

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Replies are listed 'Best First'.
Re: XML::Simple isn't simple!
by VSarkiss (Monsignor) on Aug 17, 2001 at 05:36 UTC

    ? Looks like your arrows are in the wrong place. Shouldn't that be: print $ref->{os}{Linux}{score};
    Otherwise you're referring to a nonexistent hash %ref, no?


      Well whaddya know Joe, it works. However, I don't understand the difference between the two, and why mine fails while yours succeeds! Could someone please explain to me, in detail if you don't mind, the difference between the two statements?

      And, thank you VSarkiss!

      r. j o s e p h
      "Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

        Ah, perlref is your friend. To see the difference, look only at the leftmost part of the variable. Simply put, in this line: print $ref{'os'}->{'Linux'}->{'score'};
        The first part is using a hash %ref, with the key os. (Block out the first arrow and everything that comes later and you'll see what I mean.) Since %ref is not defined at that point, it prints nothing.

        On the other hand, in this line: print $ref->{os}{Linux}{score};
        the first part is using a scalar $ref, and is using an arrow to treat it as a reference -- specifically a hash reference.

        As for leaving out the quotes, as of Perl 5.mumble, you don't need to quote strings when they're used as keys to a hash.

        GIH (Glad it helped ;-)

Re: XML::Simple isn't simple!
by ozone (Friar) on Aug 17, 2001 at 16:11 UTC
    Simple solution:
    print $ref{'os'}->{'Linux'}->{'score'};
    should be:
    print $ref->{'os'}->{'Linux'}->{'score'};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://105594]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2017-01-18 06:33 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (160 votes). Check out past polls.