# Get the data and store it in a message hash while () { chomp; my ($id, $parent, $from, $date, $content) = split /,/; $msg->{$id} = { # id => $id, # only if you feel it's necessary thread => $parent, from => $from, date => $date, content => $content, child => [], }; push @{ $msg->{$parent}[child] }, $id; # or even # push @{ $msg->{$parent}[child] }, $msg->{$id} # to save time/work/effort/etc # but then it might be necessary to store the id # in the hash itself }