while (@array)
This will not do what you expect it to do (specifically, the loop will not terminate.) Please fix your code before posting it.
Based on the rest of your code, I assume that your stanza is contained in a file, and that your data is laid out just the way you specify - tags, etc. each on a line of its own. In that case, the answer is fairly simple:
#!/usr/bin/perl -w use strict; open my $config, '+<', 'stanza' or die "stanza: $!\n"; my @all = <$config>; seek $config, 0, 0; splice @all, -1, 0, "\tMy new data\n"; print $config @all; close $config;
This will insert the "\tMy new data\n" string just before the closing stanza.
Update: Re-read the OP's code, added another option for multiple stanzas in a file.In case you need to select one of multiple stanzas by IP, here's a different version:
#!/usr/bin/perl -w use strict; open my $config, '+<', 'stanza' or die "stanza: $!\n"; my $ip = '12.34.56.78'; my $seen; while (<$config>){ if (m{^<stanza $ip>$}){ $seen = 1; } if ($seen && m{</stanza>}){ print "\tMy new data\n"; $seen = 0; } print; } close $config;
In reply to Re: Insert lines into specific stanza line
by oko1
in thread Insert lines into specific stanza line
by xjlittle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |