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."

In reply to Re: Undesired array growth by BrowserUk
in thread Undesired array growth by apok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.