You would have done well to have taken a few deep breaths and calmly considered the clear advice given by
Corion in
Re^2: Replacing duplicate string (and also
lostjimmy's
pseudocode), but since others have given detailed replies, so shall I.
You don't say how the data is held (In a scalar? An array of lines? In an unopened file?), but you and others are going with processing a file line-by-line, which generalizes nicely to processing an array, so I'll take that approach.
use warnings;
use strict;
my $bol_priority = qr{ \A priority }xms; # 'priority' begins a line
my $blank_line = qr{ \A \s* \z }xms;
my $saw_priority = '';
while (<DATA>) {
s{ $bol_priority }{bandwidth}xms if $saw_priority;
$saw_priority = /$bol_priority/ .. /$blank_line/;
print;
}
__DATA__
Class control
priority 5
Class voip
priority 30
Class video
priority 40
Class control
priority 10
Class voip
priority 25
Class video
priority 45
Output:
Class control
priority 5
Class voip
bandwidth 30
Class video
bandwidth 40
Class control
priority 10
Class voip
bandwidth 25
Class video
bandwidth 45
Update: Added reference to
lostjimmy's reply.
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.