At 1:29 AM -0800 12/24/02, Michael G Schwern wrote:
I'm going to take a left turn in replying and say that your approach to the problem is causing the problem. This is diverging from the question of tree manipulation, but I don't think that's what you really need.
Well-meant suggestions are always welcome!
So with simple data like this, I'd just use YAML. This isn't really important, just a YAML plug. :) But it does have a better resulting data structure as we'll see below.
I went to a talk on YAML and was quite impressed, overall. My main issue with it is that it isn't "buzzword-compliant". As I'm hoping to have other folks write programs to read my files at some point, this may be an issue.
- time: 123456789 processes: - pid: 123
This is definitely cleaner-looking than my XML!
And there's your problem. The data structure you've created above is not really a comfortable one in Perl. You're trying to create a Tree-like structure using array references as nodes. This is awkward. Instead, use hashes. Here's how YAML dumps the structure: my @ps_snapshots = [ { 'processes' => [ { 'stat' => 'SN+', 'pcpu' => '4.6', 'pid' => '123' },
... I can see that this structure would be far easier to traverse.
Sort of an odd structure, but ok. Here's how I'd flip around the YAML structure (again with the caveat about hyperoperators).
Again, this seems reasonable, largely because you're able to use arrays and hashes.
I think your external format (XML which is a tree) is not mapping well to your internal format (Perl which uses hashes,arrays and scalars) causing you to have to shuffle your awkward XML->tree structure into something more Perlish. By picking an external format, YAML, which maps better to your internal format you can avoid the intermediate step.
I can see this, but the issue of buzzword compliance is still there. Maybe I should just emit XML upon request, like M$ (:-).
Alternatively, I'm sure you can rewrite your XML parser to produce a structure similar to that which YAML produces. The point being to pull in your data in a way which better fits Perl.
Part of the problem in using a single example is that it can't show the entire range of possibilities. However, because YAML is a closer match than XML to Perl data structures, it should always be at least as comfortable a fit to a given problem.
Hyperoperators will help. A simplified slicing syntax, especially when dealing with references, will help. A simplified reference syntax helps, too.
I will be interested to see how all these turn out (:-).
And, of course, Perl 6 will hopefully ship with a YAML parser. ;)
Cool! -r -- email: [EMAIL PROTECTED]; phone: +1 650-873-7841 http://www.cfcl.com/rdm - my home page, resume, etc. http://www.cfcl.com/Meta - The FreeBSD Browser, Meta Project, etc. http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series http://www.ptf.com/tdc - Prime Time Freeware's Darwin Collection