$hash{$msgid} = {
filepos => $msgstart,
inreply => $msgreply,
nextmsg => [] # reference to an empty array
}
####
my @heads;
while(my ($key, $value) = each(%hash)) {
# does this msg have a parent?
if (my $parent = %hash{$value->{inreply}}) {
# link the parent to this message
push @{$parent->{nextmsg}}, $key;
}
else {
# no parent so it must become a thread start
push @heads, $key;
}
}
####
foreach my $msgid (@heads) {
# do what ever you have to do to start a new thread
# recursively follow the messages
thread_msgs($hash{$msgid});
}
# recursively read through the thread
sub thread_msgs {
my $msgid = shift;
# use seek() to position the mbox file at
# the message start using $msgid->{filepos}
# copy to the end of the message
foreach $msgid (@{$msgid->{nextmsg}}) {
thread_msgs($msgid);
}
}