# read the whole file at once # XXX: updated (thanks, jmcnamara and VSarkiss) { local $/; $file = ; } @titles = $file =~ /_\[\d]([^_]*)/g; #### # read more lines as needed while () { if (/_\[\d]([^_]*)_/) { push @titles, $1; } elsif (/_\[\d]([^_]*)/) { push @titles, $1; while () { if (/([^_]*)/) { $titles[-1] .= $1 } last if /_/; } } }