#!/usr/local/bin/perl use strict; use warnings; my %category_hash; my %categories; # Build a hash containing all of our data records... while () { chomp; my %cat; @cat{'id','name','parent'} = split /,/; $categories{$cat{id}} = \%cat; } # ... and turn it into a tree foreach $_ (keys %categories) { if ($categories{$_}{parent}) { $categories{$categories{$_}{parent}}{children}{$_} = $categories{$_}; } else { $category_hash{$_} = $categories{$_}; } } # And now %category_hash is exactly the same as it was # in my previous post. Carry on with "show" as before. __DATA__ 1,whatever,0 2,something,1 3,another subcategory,2 4,something else,0