To get the length of an array use scalar @nameList
instead of @nameList.length
.
A typical for-loop uses the less-than operator when counting up, e.g.:
for ( $to = 0; $to < scalar(@nameList); $to++ ) ...
You should never use a post-increment unless you understand the side effects. I believe the following line:
@nameList[$to] = @nameList[$to] . "_" . @nameList[$to++];
... should be written as ...
$nameList[$to] = $nameList[$to] . "_" . $nameList[$to + 1];
Finally the comparison you use should account for the boundary condition (because you refer to $to + 1
inside the loop):
if( $to < (scalar(@nameList) - 1) ) {
$nameList[ $to ] = $nameList[ $to ] . "_" . $nameList[ $to + 1 ];
}