Another way to do this - read in entire file into list, then manipulate in memory, easier to work with.
#!/usr/local/bin/perl -w
use strict;
chomp(my @lines = <DATA>);
my @newlines = reverse grep m/'\d+'/, @lines;
@lines = map { if (/'(\d+)'/) {
my $c = scalar @newlines - $1 + 1;
my $l = $1 == 1 ? "NewLine1\n" : "";
"$l(+$c) " . $newlines[$1-1] . ";fac=$c"
} else {
/Line5/ ? "$_\nMeanLine" : $_
}
} @lines;
print "$_\n" for @lines;
__DATA__
Line1
Line2
Line3
Line4
Line '1'
Line '2'
Line '3'
Line '4'
Line5
Line6
And the output -
Line1
Line2
Line3
Line4
NewLine1
(+4) Line '4';fac=4
(+3) Line '3';fac=3
(+2) Line '2';fac=2
(+1) Line '1';fac=1
Line5
MeanLine
Line6
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.