in reply to counter issues

for (my $i = 0; $i < @array; $i++)
this will run from 0 to your last used index. Resulting in:
$array[$i + 1]
accessing an undef-ined value.