Mushy-pea 36 What, you can change this tag?

Hello everyone. I'm working on getting threads of replies to work in my forum system. I'm using this function:

sub read_fields {
my($offset1, $offset2, $offset3, $offset4, $this_offset, $result, $n, $dbase, $data, @field_name, @name_length, $num_fields, $m, $check, $check2, $primary_key, $action, @passed_array);
($offset1, $offset2, $result, $dbase, $num_fields, $m, $action, $primary_key, @passed_array) = @_;
@field_name = splice(@passed_array, 0, 6); @name_length = splice(@passed_array, 0);
# $action is always 1 in the cases where things go wrong.
unless ($action == 1) {
$field_name[0] = $field_name[5];
$name_length[0] = $name_length[5];
}
for ($n = 0; $n < $num_fields; $n++)
{
$field_name[$n + 6] = "</" . substr($field_name[$n], 1);
$offset3 = index($$dbase, $field_name[$n], $offset1) + $name_length[$n];
$offset4 = index($$dbase, $field_name[$n + 6], $offset3) - 1;
unless ($n != 4) {$this_offset = $offset3}
@$result[$n] = substr($$dbase, $offset3, $offset4 - $offset3 + 1);
}
return $this_offset;

}

to parse an XML file with fields like this:

<record>
<links>#either some text or just spaces</links>
<post_id>000000048</post_id>
<subject>#some text</subject>
<author>#some text</author>
<data+time>#some text</date+time>
<content>#some text</content>
</record>

So, the read_fields() sub is passed the number and names of fields to read and an offset telling it where to start searching in the file. This works in most cases, but in some situations it gets the <date+time> field wrong, while getting all others right. It either reads the field from the wrong record or comes back blank. I've started planning a work around to deal with this, but I'm troubled that I can't see where the bug is in there (and a patch is never the best solution). Can anyone see a gaping hole in my logic here? Any advice appriciated.

Steven.