On 4/18/07, Gauthier, Dave <[EMAIL PROTECTED]> wrote:



Hi Jay:

Going back a couple weeks regarding the nested foreach to read out nested
data using XML::Simple, your solution works great, Thanks !

But a new requirement is that I sort the output


foreach (@{$xml->{FOREST}}) {

    print $_->{NAME}, ":\n";

    foreach (@{$_->{TREE}}) {

        print "\t", $_->{NAME}, ":\n";

        foreach (@{$_->{DIAM_HEIGHT}}) {

            print "\t\ttdata:\t", $_, "\n";

        }

    }

}


To be painfully honest, I'm not sure what's going on inside the...

[EMAIL PROTECTED]>{FOREST}}} (for example).  Is there some sort of casting 
going on
here?  I tried to "sort" the list, and it ran, but the output was not
sorted.  I suspect that it's sortig pointers and not values :-(

Any help would be appreciated.

Thanks (Again)

-dave


Sort of. Perl references aren't pointers, per se. They're references
to to internal data structures, not actual memory locations. But
you're on the right track.

$xml is a scalar that contains a reference to an anonymous hash
structure. In your case, there is only one key/value pair in the hash,
"FOREST". So far, we could write that as:

   my $xml = { FOREST => "value"};

If you're not comfortable with references, you should take another
look at the perlreftut and perlref man pages.

The value of FOREST, in turn, is a reference to an anonymous array of
all the forests. The anonymous array of forests, in turn, contains an
anonymous hashref with information on each forest. Something like
this:

   my $xml = { FOREST => [{}

So

  @{$xml->{FOREST}}

says, "give me the array that is the value of $xml->{FOREST}". Since
@{$xml->{FOREST}}, is a list of hashrefs, though, it's not much use by
itself. That's why we cycle through it with the foreach. For each
element of @{$xml->{FOREST}}, $_->{NAME} is the name of the forest,
$_->{TREE} is the reference to the anonymous list of trees, etc.

To sort the forests, you'll need to sort based on the value of
$_->{NAME}. sort() supports that, since $a and $b are simply
references to each of the values of the list being sorted:

   foreach ( sort { $a->{NAME} cmp $b->{NAME} } @{$xml->{FOREST}} ) {
       print $_->{NAME}, "\n";
   }

see perlfunc for information on sort.


If this sounds complicated, it is. Because XML is a markup language,
each element of a similar type has the same label. This makes it very
easy to apply attributes to all elements of a specific type, or count
how many elements of a specific type you have. If you want to do
something to a particular element, though, you run into trouble. If
all your forests are just called <forest></forest>, it's hard to know
which <forest> is which. XML::Simple solves this by aggregating all
the elements at a particular level of the XML parse tree into
anonymous array. $xml->{FOREST}->[0] is the first forest,
$xml->{FOREST}->[1] the second, etc.

Looking at the contents of $xml with Data::Dumper as I suggested
before would help make things clearer.

HTH,

--jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to