CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Algorithm in Perl

 



AggressiveFish
Novice

Jun 7, 2011, 9:15 AM

Post #1 of 7 (1481 views)
Algorithm in Perl Can't Post

Here's a DTD I have but I've been having some trouble parsing it and generating a hash in Perl from it. I want to parse a xml document with the below layout and end up having a hash with the data stored. The xml document is essentially a tree of data, some branches longer than others and varying number of child nodes.


The end hash should consist of a hash hierarchy of only the values from the "id" elements. blockInfo contains information about the current treeTop item. So a treeTop item has an id instead of blockInfo that is unique and a treeTop item can contain more treeTop items, or in this case I guess you can call branches.


Any help would be appreciated. Thanks.

<c>

<!DOCTYPE TREELAYOUT [

<!ELEMENT treeMap (treeTop*)>

<!ELEMENT treeTop (blockInfo, treeTop*)>

<!ELEMENT blockInfo (id, name, description, field1)>

<!ELEMENT id (#PCDATA)>

<!ELEMENT name (#PCDATA)>

<!ELEMENT description (#PCDATA)>

<!ELEMENT field1 (#PCDATA)> ]>

</c>


(This post was edited by AggressiveFish on Jun 7, 2011, 9:21 AM)


miller
User

Jun 7, 2011, 9:19 AM

Post #2 of 7 (1478 views)
Re: [AggressiveFish] Algorithm in Perl [In reply to] Can't Post

To process XML, I pretty much always use XML::Twig or XML::Simple.

- Miller


AggressiveFish
Novice

Jun 7, 2011, 9:24 AM

Post #3 of 7 (1476 views)
Re: [miller] Algorithm in Perl [In reply to] Can't Post

I'm using XML::Simple. I need an algorithm that will end up with a hash made up of only "id" values from the xml document so I need to parse it. Some "treeTop"s may have no treeTop tags under it, one treeTop tag, or many.


rovf
Veteran

Jun 8, 2011, 1:02 AM

Post #4 of 7 (1456 views)
Re: [AggressiveFish] Algorithm in Perl [In reply to] Can't Post

But the hash which is delivered by XML::Simple, contains the whole XML structure as a tree, so you only have to traverse this tree recursively to find the "id" data.

An alternative could be to use XML::XPath, but I don't have
experience with XPATH, so I don't no for sure whether your
requirements can be easiliy encoded as XPATH expressions.


AggressiveFish
Novice

Jun 8, 2011, 3:48 AM

Post #5 of 7 (1453 views)
Re: [rovf] Algorithm in Perl [In reply to] Can't Post

But that's my question. I want to know how to go through the hash level by level and create the hash. If I only go 2 levels it's fine but somehow I'm losing levels as I go farther down. I think it's cause I'm overwriting levels when I come back. I make a recursive call to the same routine to handle the next branch in the tree. I'm wondering what I need to do to recursively call a routine and have it parse and generate the hash I need.


rovf
Veteran

Jun 8, 2011, 4:38 AM

Post #6 of 7 (1452 views)
Re: [AggressiveFish] Algorithm in Perl [In reply to] Can't Post

It's hard to say where you are making a mistake, because you have not posted any code. The idea is correct that you need to go recursively through the tree (maybe unless you want to use XPATH).


AggressiveFish
Novice

Jun 8, 2011, 5:53 AM

Post #7 of 7 (1448 views)
Re: [AggressiveFish] Algorithm in Perl [In reply to] Can't Post

Hmm.... maybe I should ask this. If I know I'll need to start setting values (let's say 3 levels into a hash) but the second level isn't set (doesn't exist) what's the best way to "initialize" it? So if I try to set $hash{"level1"}{"level2"}{"level3"} without "level2" existing should I set it to something? Right now I do a check to see if it exists and then do $hash{"level1"}{"level2"} = {} but I end up getting a "" entry in the hash that I don't want. I think this is where my code is mixed up a bit. I've moved too much around trying things out to post right now but if someone could give me some feedback on how to initialize a level not created in a case like above I'd appreciate it. If it doesn't work out I'll get the code back to initial state and post the subroutine I'm using. Thanks again.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives