Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not reinventing the wheel

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I am reading from a flat file database with the following structure

ID | ParentID | NAME
ID | ParentID | NAME
ID | ParentID | NAME
ID | ParentID | NAME


I am trying to avoid reinventing the wheel here. I now it is possible to iterate through the array an build a tree.

I am trying to create and xml like output with opening and losing tags like so.
In this case the opening tag is "item[" and the closing tag is "]"

item[ { type:name }
{ type:name }
item [ { type:name }
{ type:name }
]
]

Any suggestions on how to organize my code or has someone done this before so I dont have to reinvent the wheel.

Thanks in advance


haunter@battlestrata.com
 
Hi Haunter - sounds as if what you need are hashes of hashes etc. Have a look at the Data Structures Cookbook file://C:\Perl\html\lib\Pod\perldsc.html on my system, probably on yours as well.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Also look up XML on search.cpan.org, chances are you're not the first to try what you're doing
;-)

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
I was thinking hashes as well MikeLacey as I only managed to learn how to use these yesterday so they're fresh in my mind. Some simple points to remember for hashes:
1. Create an empty hash:
Code:
my %hash = ();
2. Hashes use keys to reference the elements, unlike arrays which use scalar values (correct me if I'm wrong).
Code:
$hash{ $key } = { 'value1' => $value1, 'value2' => $value2 };
3. To loop through them use something like:
Code:
for my $key (keys %hash)  {
   my $value = $hash{$key};
   print "$key => $value\n";
}
I found a good web site all about hashes at:

Rob Waite
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top