in reply to Undesired array growth

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 ] ) {

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Undesired array growth
by apok (Acolyte) on Jan 09, 2009 at 06:10 UTC

    Your version exits the for loop correctly! It has a glitch or two in the concatenation, but I should be able to combine the logic of the two into one completely working script. Many, many thanks. =)

    EDIT: though really, all the concat logic was from my script in the first place so it was my bad it's not working completely correct. Silly me.