in reply to Insert newline

I see what you are trying to do here. The while ( <DATA> ) loop iterates through lines of the data file, and then you are creating more lines inside the loop that you want to iterate through them as if they were additional lines in the data file. Unfortunately, you would have to add them to the actual data file for that effect. To do what you want, you can add another loop to iterate through the new lines you've added:

!/usr/bin/perl use strict; use warnings; my $title; while ( <DATA> ) { chomp; if ( /\(\w+\-\d+\)$/ ) { $title = $_; } else { s/(?:^|\s)(\d\-\d\w{2}(?:\.\w+)?|[A-Z]\d{2}(?:\.\d+)?)/\n$1/g; foreach my $line ( split ( "\n" ) ) { next if $line !~ /^(\d\-\d\w{2}(?:\.\w+)?|[A-Z]\d{2}(?:\.\d+)?)/ +; print "$title;$line\n"; } } } __DATA__ Titel Text (A12-3) 3-123.7 Just another text 3-123.8 Some more text A12.34 Another item B56.78 Yet another item Another Titel Text (B23-9) 1-22a.b Just another text 2-3cd.e Some more text W12.34 Another item Z56.78 Yet another item Some trash Some trash

Output:

Titel Text (A12-3);3-123.7 Just another text Titel Text (A12-3);3-123.8 Some more text Titel Text (A12-3);A12.34 Another item Titel Text (A12-3);B56.78 Yet another item Another Titel Text (B23-9);1-22a.b Just another text Another Titel Text (B23-9);2-3cd.e Some more text Another Titel Text (B23-9);W12.34 Another item Another Titel Text (B23-9);Z56.78 Yet another item

Replies are listed 'Best First'.
Re^2: Insert newline
by Anonymous Monk on Sep 14, 2011 at 18:17 UTC
    Thank you so much!!!
    I'll test this on the "big file" now.
    Many many thanks!
    VE