Assuming I've made no mistakes, this should replicate what your current code is doing, including the errors.
But (IMO) it is a little simpler (uses splice), and rather clearer, and may allow you to see your logic error?
use constant { NAME=>0, TYPE=>1, DETAILS=>2 }; open OUT, ">${new_file}.meta" or die "DIAF OUT: $!"; for( my $page = 0; $page < @meta; ++$page ) { for( my $line = 0; $line < @{ $meta[ $page ] }; ++$line ) { print "$page:$line/",scalar @{ $meta[ $page ] },"\n"; ## while the NAME is defined ## and neither TYPE nor DETAILS are while( $meta[ $page ][ $line +1 ][ NAME ] and ! $meta[ $page ][ $line +1 ][ TYPE ] and ! $meta[ $page ][ $line +1 ][ DETAILS ] ) { print "while loop #1\n"; ## Concatenate the NAME from the next line ## to the current line $meta[ $page ][ $line ][ NAME ] .= ' ' . $meta[ $page ][ $line +1 ][ NAME ]; ## and delete the next line splice @{ $meta[ $page ] }, $line +1, 1; } ## while TYPE is undefined, but DETAILS are ## and there is another line in this page while( ! $meta[ $page ][ $line +1 ][ TYPE ] and $meta[ $page ][ $line +1 ][ DETAILS ] and $line +1 < @{ $meta[ $page ] } ) { print "while loop #2\n"; ## Concatenate the NAME from the next line ## to the current if it exists $meta[ $page ][ $line ][ NAME ] .= ' ' . $meta[ $page ][ $line +1 ][ NAME ] if $meta[ $page ][ $line +1 ][ NAME ]; ## concatenate the details from the next line ## to the current $meta[ $page ][ $line ][ DETAILS ] .= ' ' . $meta[ $page ][ $line +1 ][ DETAILS ]; ## and delete the next line splice @{ $meta[ $page ] }, $line +1, 1; } ## if the next line is undefined if( ! $meta[ $page ][ $line +1 ] ) { print "last if check\n"; ## while the first line of the next page ## has no TYPE but does have DETAILS while( ! $meta[ $page +1 ][ 0 ][ TYPE ] and $meta[ $page +1 ][ 0 ][ DETAILS ] ) { print "while loop #3\n"; ## concatenate the NAMEs if it exists ## in the first line of the next page $meta[ $page ][ $line ][ NAME ] .= $meta[ $page +1 ][ 0 ][ NAME ] if $meta[ $page +1 ][ 0 ][ NAME ]; ## And the DETAILS $meta[ $page ][ $line ][ DETAILS ] .= $meta[ $page +1 ][ 0 ][ DETAILS ]; ## And delete the first line of the next page splice @{ $meta[ $page +1 ] }, 0, 1; } } print OUT join( "\t", @{ $meta[ $page ][ $line ] }[ NAME, TYPE, DETAILS ] ), "\n"; } } close OUT;
I can not, in the abstract, see the error in your logic, but maybe a different view of your code will make it stand out for you.
Update: One thing that looks wrong is that you are checking whether another line exists, after you've already looked at the elements of that line:
## while TYPE is undefined, but DETAILS are ## and there is another line in this page while( ! $meta[ $page ][ $line +1 ][ TYPE ] and $meta[ $page ][ $line +1 ][ DETAILS ] and $line +1 < @{ $meta[ $page ] } ) {
That would probably be better as:
## while there is another line in this page ## and its TYPE is undefined, but its DETAILS are while( $line +1 < @{ $meta[ $page ] } and ! $meta[ $page ][ $line +1 ][ TYPE ] and $meta[ $page ][ $line +1 ][ DETAILS ] ) {
In reply to Re: Undesired array growth
by BrowserUk
in thread Undesired array growth
by apok
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |