Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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
[thezip]: Our cat is single-handedly (pawed-ly?) solving our field mouse problem. About four critters in the last week alone.
[thezip]: Amazing speed and stealth in that one.
[GotToBTru]: You can't tunafish, but some seafood companies can tunafish. More fun with English.
[virtualsue]: a veterinarian told me to cut down on my cat's food. i said, when i do that she just catches more mice
[thezip]: My dogs are also excellent mousers, at least that's what they tell me.
[GotToBTru]: our beagle has dispatched 3 rabbits to the Great Hutch in the Sky since March.
[thezip]: I can always tell when the dogs have been mousing, as there is a concentric ring of mud around the end of their snouts when they do.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2017-11-17 19:53 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.