OK, so if you are familiar with project management software, you know that resources belong to roles, etc...

So a normal hierarchy would look like the following.

Business Unit
-->General Craft
------>Carpenters
-->Engineering
------>Mech. Engineers

etc...

I'm exporting this hierarchy of roles to a text file, seeing as how I'm going through an activity list with countless rows of data, resources repeat constantly.

So in my text file I want to export the hierarchy of roles with out repeating lines.


Example

Activities
Act Num.-----Act Name-------Resource
---10-------------Do This--------Carpenter
---20-------------Do That--------Mech. Engineer
---30-------------Do Over-------Carpenter


So analyzing this line by line should spit out the hierarchy listed above to my text file if I have another table/sheet outlining that Carpenters belong to Gen. Craft and Gen. Craft belongs to Business Unit, and the same with Mech. Engineers -> Engineering -> Business Unit. Both resources ultimately belong to the Business Unit container.

But how do I do this line by line analysis and make the algorithm smart enough to not repeat the process when a resource is repeated or belongs to the same hierarchical container as another resource? This is Tough. Think of it as a brain teaser.
Received Infraction